user3354539
user3354539

Reputation: 1245

html select list - get the displayed text value

I have a bootstrap modal confirm pop up connected to a form submit button where I want to include the current text value displayed in a html select list as well as the selected text value of the same html select list.

For example, I have the following html select list:

<select name="test_list" id="id_test_list">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
    <option value="four" selected="selected">Four</option>
    <option value="five">Five</option>
</select>

To add the modal message to the submit button, I have the following code:

$( "#submit_buttonA" ).attr('update-confirm', 'From ' + $('#id_test_list option:selected').text() + ' to ' + $('#id_test_list').text());

The modal message is displayed as:

From One to One Two Three Four Five

How do I change the code of the 2nd text value $('#id_test_list').text() to display only the current text value of the html select list?

For example, the user has displayed the option value two (Two) in the html select list (but the selected value is still option value four).

From Four to Two

EDIT I have just realized what I have asked here! This is the effect of being sleep deprived and looking at a problem for too long and over thinking the issue. Sorry to all for asking such a deranged question.

Upvotes: 0

Views: 2043

Answers (3)

R.K.Bhardwaj
R.K.Bhardwaj

Reputation: 2192

replace

submit_buttonA

to this

id_test_list

Upvotes: 0

Loren Posen
Loren Posen

Reputation: 235

Change .text() to .val().

$('#id_test_list').val();

Upvotes: 1

Mohit S
Mohit S

Reputation: 14064

I am not sure what you are trying to do. but as you want the current or selected value of the select you can use

$( "#submit_buttonA" ).attr('update-confirm', 'From ' + $('#id_test_list option:selected').text() + ' to ' + $('#id_test_list').val());

Change text() to val()


Alternatively you can also use

$('#id_test_list :selected').text(); 

Upvotes: 2

Related Questions