48716

How to change the value of the currently selected item in the list box?

Question:

I know one can get the value of the currently selected item like this:

var myListBoxItemText = $('#myListBox').val().toString();

But how do you change this value in the list box to something else?

Answer1:

Seeing your update in the comments, here's an updated answer. Just use the <a href="http://api.jquery.com/selected-selector/" rel="nofollow">:selected</a> selector to filter the options inside of your select element to get the selected option.

To change a select element's selected option's value property:

$('#myListBox option:selected').val('new value');

To change its display text:

$('#myListBox option:selected').text('new text');

<em><strong><a href="http://jsfiddle.net/ult_combo/NubSF/" rel="nofollow">JSFiddle</a></strong></em>

Answer2:

$("#myListBox").val("This is the new value");

Although, by 'listbox', I assume you mean a select element. In which case you'll have to add the property selected to one of the child option elements.

Answer3:

$('#myListBox').val(your_val);

Here your_val denotes the value attribute of <option>

Or you can also use

$('#myListBox')[0].selectedIndex = 1;

or

$('#myListBox').prop('selectedIndex', 1);

<strong><a href="http://jsfiddle.net/thecodeparadox/QStkd/175/" rel="nofollow">DEMO</a></strong>

<strong><a href="http://jsfiddle.net/thecodeparadox/QStkd/176/" rel="nofollow">DEMO 2</a></strong>

Recommend

  • How to replace TouchesBegan with UIGestureRecognizer
  • how to remove a div with same ids but display='block' and display='none' in JAVa
  • Jquery popup on mouse over of calendar control
  • How to revert to previous XCode version?
  • Ajax calls do not work in IE unless you fiddle with security settings
  • C: Incompatible pointer type initializing
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Keep this build forever option - Jenkins
  • Do I need to reset a Perl hash index?
  • Use of this Javascript
  • dc-js disable selecting slices on click for pie chart
  • Spring security and special characters
  • Deleting and Updating values from a cusrsor adapter
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Modifying destination and filename of gulp-svg-sprite
  • MySQL WHERE-condition in procedure ignored
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • How to show dropdown in excel using jrxml (jasper api)?
  • Display Images one by one with next and previous functionality
  • Weird JavaScript statement, what does it mean?
  • ORA-29908: missing primary invocation for ancillary operator
  • Jquery - Jquery Wysiwyg return html as a string
  • How to get next/previous record number?
  • Confusion with PayPal's monthly billing cycle
  • Arrays break string types in Julia
  • retrieve vertices with no linked edge in arangodb
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • WPF Applying a trigger on binding failure
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • How do you join a server to an Active Directory (domain)?
  • Understanding cpu registers
  • Java static initializers and reflection
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Add sale price programmatically to product variations
  • Django query for large number of relationships
  • Observable and ngFor in Angular 2
  • Python/Django TangoWithDjango Models and Databases
  • git trying to push non-existent file … after clearing cache