index.php i am using:
<script src="srcipt.js"></script>
<div id="demo">
<h3><input type="hidden" name="id" value='1' />test1</h3>
<h3><input type="hidden" name="id" value='2' />test2</h3>
<h3><input type="hidden" name="id" value='3' />test3</h3>
<h3><input type="hidden" name="id" value='4' />test4</h3>
<h3><input type="hidden" name="id" value='5' />test5</h3>
</div>
in script.js
$(document).ready(function(){
$('#demo h3').click(function(){
var id = $(this).parent().find('input[type="hidden"][name="id"]').val();
alert(id);
});
});
When I click on “test1” result 1
, click on “test2” result 1
…
, click on “test5” result 1
How to get value when click on “test2” is result 2, how to fix it ?
That should work!
$(this)refers to your h3 element. Why would you go to the parent first to search for an input? You can just get the first input element of the selected h3 element!Also, please note that give multiple
inputelements the samenameis a bad practise in most cases.