MasterSinge
MasterSinge

Reputation: 675

How to get the value of random id using JQuery?

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.

Exemple :

First REFRESH => id = "select2-account-t4-container"

Second REFRESH => id = "select2-account-g8-container"

And so on...

Upvotes: 4

Views: 478

Answers (2)

V.Tur
V.Tur

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

Zakaria Acharki
Zakaria Acharki

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

Related Questions