<input type="button" id="one" value="1" OnClick='addValue("function(){document.getElementById("one")}")'>
I want to pass the id of this input as argument of addValue function. Where is the mistake here ?
您太复杂了,使用this
<input type="button" id="one" value="1" OnClick='addValue(this.id)'>
First, move the event listener out of the HTML and then you can access the value from the click events target.
document.getElementById('one').addEventListener('click', function(evt) {
var value = evt.target.value;
addValue(value);
});
Edit: or use @tymeJV's solution, though I'd recommend getting away from inline events.
If you really want to use function as argument of another function then have a look at anonymous functions http://en.wikibooks.org/wiki/JavaScript/Anonymous_Functions , SetTimeout is a good example, you pass a function as an argument to it to call after specific time.
But I think you can accomplish this without passing function as argument as mentioned in the answer of @tymeJV.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.