javascript - Can an event be triggered on a click on a item marker on an html unoredered list? -
is there way trigger action when user clicks on the icon of item in unordered list? (in javascript and/or jquery)
yes, can target list item marker if wish, requires wrapping contents of each item in tag.
this works because when assign event handler <li>
element, element or kid elements trigger handler. within handler can check nodename
, tell if <li>
clicked or if kid element. since contents wrapped <span>
or <div>
, list item marker trigger event target beingness <li>
.
the next runnable illustration trigger alert when list item markers clicked, otherwise click event ignored.
class="snippet-code-js lang-js prettyprint-override">$("li").on('click', function(e) { if (e.target.nodename === "li") { alert('you clicked list item marker'); } else { e.preventdefault(); //a kid element clicked } });
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li><span>one</span></li> <li><span>two</span></li> <li><span>three</span></li> </ul>
javascript jquery
No comments:
Post a Comment