Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get value multiple tags in javascript using the id?

I want to get the name of the two <p> elements in my html file using id after the button is clicked. but in my code, when I clicked the button, i it only outputs "print". How can I do this?

This is my HTML code:

<p id="name" name="print">hey</p>     
<p id="name" name="this">hey</p>     
<button id="btn11">test11</button>

This is my javascript code:

$("#btn11").click(function(){
    alert($("#name").attr("name"));
});
like image 539
Jemuel Elimanco Avatar asked Feb 23 '26 05:02

Jemuel Elimanco


1 Answers

use class instead of ID and

<p class= "name" name = "print">hey</p>

<p class= "name" name = "this">hey/p>

<button id="btn11">test11</button>

$("#btn11").click(function(){ 
  $('.name').each(function () {
   alert( $(this).attr('name'));
  }); 
});
like image 172
Nagesh Sanika Avatar answered Feb 25 '26 17:02

Nagesh Sanika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!