Reputation: 5
I have two results in my select db which are text_1 and text_2 they have different ids, but when I click text_2 it alerts the id of text_1
my php echo
echo'<p><input id="textid" type="hidden" value="'.$msg->id.'">'.$msg->text.'</p>';
this is my js code
var getid= document.getElementById('textid').value;
$("p").click(function(){
alert(getid);
});
Upvotes: 0
Views: 406
Reputation: 2354
PHP
echo'<p><input id="textid_1" type="hidden" value="'.$msg->id.'">'.$msg->text.'</p>';
echo'<p><input id="textid_2" type="hidden" value="'.$msg->id.'">'.$msg->text.'</p>';
JS
$("p").click(function(){
alert($(this).find("input").val());
});
Upvotes: 1
Reputation: 21882
You can't use an id
that is the same for both inputs. ID
s should be unique, only 1 id per page. But I'm guessing since you didn't post more than 1 line of HTML.
I changed your id
to class
on the input tag.
Then move the variable inside the click function to target the clicked p
. And the proper way to get an input value is to use .val()
not .value
.
$("p").click(function(){
var getid= $(this).children('.textid').val();
alert(getid);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p><input class="textid" type="hidden" value="VALUE IS 1">'.$msg->text.'</p>
<p><input class="textid" type="hidden" value="VALUE IS 2">'.$msg->text.'</p>
Upvotes: 3