javascript - How to target more than one element using JS? -


 function showpage(){    	var id=document.getelementbyid("link").getattribute("href");       alert(id);    }
<a href="#link1" id="link" onclick="showpage();">div 1</a><br>  <a href="#link2" id="link" onclick="showpage();">div 2</a><br>

somebody please me finding issue

on clicking link alerts #link1 not showing #link2

you can't repeat id's in page. if want target element event occurs on pass in this parameter in html.

then use argument in function element represented this

html

<a href="#link1" id="link-1" onclick="showpage(this);">div 1</a><br> <a href="#link2" id="link-2" onclick="showpage(this);">div 2</a><br> 

js

function showpage(element){     var hash = element.hash;     // or     var hash = element.getattribute('href');         alert(hash); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -