简体   繁体   中英

Value is not defined when trying to call with function with onclick

When im trying to send(onclick) value to toggle_sprint() function i get error that value is not defined. Why is it not defined? I thought it got defined when i put value="false" but apparently not.

<div class="select">
    <div id="toggle_sprint" value="false" onclick="toggle_sprint(value);" >Toggle Sprint</div>

</div>

There is no value attribute for div elements… and div elements aren't designed to be interactive so you're creating a bunch of accessibility issues by slapping onclick on a div.

Additionally, your use of an intrinsic event attribute means that toggle_sprint() probably isn't what you think it is .

Start by writing good HTML.

Use data-* attributes to provide data just for your JS. Use buttons to provide things to click on and trigger JS.

Then bind your event handlers with JavaScript.

  document.getElementById("toggle_sprint").addEventListener("click", function(event) { toggle_sprint(this.dataset.value); }); function toggle_sprint(arg) { console.log(arg); } 
 <div class="select"> <button type="button" id="toggle_sprint" data-value="false">Toggle Sprint</button> </div> 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM