Javascript onclick for class and name -
I'm new to JavaScript and I'm sure how to do the following:
I've got a Only the CSS "class" but with the different "name" attributes, using two links incredible javascript, I need to individually separate each task. Is there any way to do this?
Example Code:
& lt; One class = "classname" name = "link 1" & gt; Link 1 & lt; / A & gt; & Lt; One class = "classname" name = "link 2" & gt; Link 2 & lt; / A & gt; Let's say I have "this link 1" for the console when I click on link 1. And "this link is 2" when the link 2 is clicked.
Attach an event handler to the elements, and only check the name and whatever you want
var elems = document.querySelectorAll ('class name'); (Var i = elems.length; i--;) {ames [i] .addEventListener ('click', Fn, false); } Function fn () {if (this.name == 'link1') {console.log ('this is link 1'); } And if (this.name == 'link2') {console.log ('this link is 2'); }}
Comments
Post a Comment