How to get random id value using jQuery?

I want to get the text value of a dropdown. But my identifier changes after every page that refreshes. I would like to know how to get the text value of an identifier that changes after each update using jQuery.

Example:

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

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

Etc...

+4
source share
2 answers

You can use start with a selector ^=like:

$('[id^="select2-account"] option:selected').val(); //value
//Or
$('[id^="select2-account"] option:selected').text(); Text

This way you will always focus on the static static part in id.

+2

html- , . $(el).val() .

$(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());
        });
    });
0

Source: https://habr.com/ru/post/1691399/


All Articles