html - How to put alert on my all anchor tag using javascript -


how put alert on anchor tag using javascript , alert box show link & text of anchor tag

you can document.getelementsbytagname, returns htmlcollection of elements given tag name.

eventtarget.addeventlistener method registers specified listener on eventtarget it's called on.

var anchors = document.getelementsbytagname("a");     (var = 0; < anchors.length ; i++) {     anchors[i].addeventlistener("click",          function (event) {             event.preventdefault();             alert(this.href);             alert(this.innerhtml);         }, false); } 

fiddle

note: have use event.preventdefault(), cancel anchors default behaviour , addeventlistener supported in ie9+


Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -