Kjensen
Kjensen

Reputation: 12374

Use Jquery to alter an item in a selectlist?

Before:

<select id="NumberId" name="NumberId">
  <option value="">ZERO</option>
  <option value="4">FOUR</option>
  <option value="5">FIVE</option>
</select>

Using JQuery, modify the value of the option with an empty value to 0 (zero).

After:

<select id="NumberId" name="NumberId">
  <option value="0">ZERO</option>
  <option value="4">FOUR</option>
  <option value="5">FIVE</option>
</select>

How can I do that?

Upvotes: 0

Views: 93

Answers (3)

Dominic Rodger
Dominic Rodger

Reputation: 99751

$("select option[value='']").attr("value", "0");

Upvotes: 1

cletus
cletus

Reputation: 625037

Well you can do something like this:

$("#NumberId option[value='']").attr("value", "0");

or

$("#NumberId option[value='']").val("0");

(not 100% sure that works with <option> elements)

But the more obvious thing to do is correctly generate the HTML on the serverside, no?

Upvotes: 1

Paul
Paul

Reputation: 5576

I think you can do something like:

$("#NumberId option[value='']").val('0');

good luck

Upvotes: 2

Related Questions