Reputation: 675
I want to get the text value of dropdown selected. But my id change after each refreshing page. I would like to know how can I get the text value of an id who is changing after each refresh, using jQuery.
First REFRESH => id = "select2-account-t4-container"
Second REFRESH => id = "select2-account-g8-container"
And so on...
Upvotes: 4
Views: 478
Reputation: 1427
If you may get html tag select by Id or in other way, you no need to use id inside them. Enough apply $(el).val() and get selected value.
$(document).ready(function() {
var el = $('#select_el');
//value after page rendering
console.log(el.val());
//value after select event
el.change(function(){
console.log(el.val());
});
});
Upvotes: 0
Reputation: 67505
You could use the start with selector ^=
like :
$('[id^="select2-account"] option:selected').val(); //value
//Or
$('[id^="select2-account"] option:selected').text(); Text
This way you will always focusing on the fixed static part in your id
.
Upvotes: 2