kmansoor
kmansoor

Reputation: 4345

jQuery select-box: getting the text when #id not known

From jquery docs:

$( "#myselect" ).val();

Will give the value and for text following will suffice:

$( "#myselect option:selected" ).text();

How can text be extracted when all one has is the this reference?

$(document.body).on("click", ".lots-of-select-boxes-class", function() {
    var value = $(this).val();
    var text = HOW-TO-GET-TEXT; // ???      
});

Upvotes: 0

Views: 40

Answers (1)

.find()

var text =$(this).find("option:selected").text();

or

.children()

var text =$(this).children("option:selected").text();

this keyword

Upvotes: 1

Related Questions