Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript - button needs to be click twice for onclick to trigger

Why does my button need to be clicked twice for the onclick event to trigger? There're some other thread on stackoverflow with the same problem, but in all the thread i found, the original poster puts the event handler inside the function. It's not like that with my code.

Html

<body>
    <ul id="parentList">
        <li>First child</li>
        <li>Second child</li>
        <li>Third child</li>
        <li>Forth child</li>
        <li>Fifth child</li>
    </ul>
    <button type="button" id="delete">Delete first child</button>
</body>

Script:

var parentList = document.getElementById("parentList");
document.getElementById("delete").onclick = function() {
    parentList.removeChild(parentList.firstChild);
};

Demonstration: onclick-error

like image 797
Nhu Thai Sanh Nguyen Avatar asked Apr 26 '17 13:04

Nhu Thai Sanh Nguyen


People also ask

Why does the button from ASP NET click twice?

Please Sign up or sign in to vote. the button from asp.net once pressed could eventually trigger twice action because at the HTML Interface there's a javascript ONCLICK properties. Now it's just run once, nor twice.

How do you use onClick event in JavaScript?

How to Use the onclick event in JavaScript The onclick event executes a certain functionality when a button is clicked. This could be when a user submits a form, when you change certain content on the web page, and other things like that. You place the JavaScript function you want to execute inside the opening tag of the button.

When should onclick function be called?

It would be good to re-open this as an issue that still needs to be fixed, encouraging others to step in and offer a PR. I feel that if you add an onClick function to a component, then it should be called when that component is clicked, consistently in all browsers.

What is wrong with my button click handler?

There is nothing wrong with your click handler on the button. The problem is with the way you are removing the first child. If you add this small change to your function: You will notice that the first click logs out some empty spaces with a line break.


2 Answers

The first "element" within the parentList is whitespace. You can see this by console logging the element within the event listener.

whitespace example

You therefore need to only filter out the li elements within the parent item.

document.getElementById("delete").onclick = function() {
    var listItems = document.querySelectorAll("#parentList > li");

    if (listItems.length > 0) {
        listItems[0].remove();
    }
};

https://jsfiddle.net/ofLvac32/13/

You could also use parentList.firstElementChild instead of parentList.firstChild, which filters out any invalid nodes (whitespace).

An example of this

var parentList = document.getElementById("parentList");

document.getElementById("delete").onclick = function() {
    parentList.removeChild(parentList.firstElementChild);
};

https://jsfiddle.net/ofLvac32/37/

like image 149
Toby Mellor Avatar answered Nov 14 '22 21:11

Toby Mellor


With elementChild, you look for any child, including white spaces.

You could use parentList.firstElementChild instead to get the first child element.

like image 31
apires Avatar answered Nov 14 '22 20:11

apires