Reputation: 49
I have this html code. I want to get the id value by the input value. For example: I want to know the id value which have an value=number2 => This is the unknown2. How can i do this with jQuery?
<th id="unknown1" rel="0">
<input id="btnSendMessage" type="button" value=number1 class="red"/>
</th>
<th id="unknown2" rel="0">
<input id="btnSendMessage" type="button" value=number2 class="red"/>
</th>
<th id="unknown3" rel="0">
<input id="btnSendMessage" type="button" value=number3 class="red"/>
</th>
Upvotes: 2
Views: 56
Reputation: 72269
You can do it like below:-
$("input[value='number2']").parent().attr('id')
Example:-
$(document).ready(function(){
console.log($("input[value='number2']").parent().attr('id'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th id="unknown1" rel="0">
<input id="btnSendMessage" type="button" value=number1 class="red"/>
</th>
<th id="unknown2" rel="0">
<input id="btnSendMessage" type="button" value=number2 class="red"/>
</th>
<th id="unknown3" rel="0">
<input id="btnSendMessage" type="button" value=number3 class="red"/>
</th>
</tr>
</table>
Note:-
<th>
without <table></table>
will not rendered on browser and hense it will be undefined
. If you want to make it work then <th>
must be inside <table></teable>
.
Upvotes: 1
Reputation: 816
var id = $('input:text').filter(function(){
return this.value === "number2";
}).prop('id');
Upvotes: 2