Amit Battan
Amit Battan

Reputation: 41

.val() of jquery not working for select element on IE

I tried all these for selected value of select element

inventory_rule = $("#inventory_rule :selected").attr('value');
inventory_rule = $("#inventory_rule option:selected").val();
inventory_rule = $("#inventory_rule").attr('value');
inventory_rule = $("#inventory_rule").val();

these all worked well in mozilla but not in IE

is any alternative

Upvotes: 4

Views: 8311

Answers (3)

Saechel
Saechel

Reputation: 152

Just encountered this earlier.

when getting the value of a

<select>

tag IE 10 inserts spaces before and after the value. so just trim the value and it will solve your worries.

var inventory_rule = $.trim($("#inventory_rule").val());

Upvotes: 1

Andy E
Andy E

Reputation: 344547

The 4th of your attempts is the most straightforward way to get the selected value of a select element and it should work.

var inventory_rule = $("#inventory_rule").val();

I wrote a quick and dirty example for you at jsfiddle.net, showing that it works. This means that your selector is probably wrong. Check and make sure the select element has id="inventory_rule" and make sure that id attribute is also unique on the page. Don't forget the var keyword if it's the first time you're declaring the variable.

EDIT: highlighted the part about making sure the id attribute is unique, a non-unique id will definitely cause problems in IE.

Upvotes: 2

Tomas Aschan
Tomas Aschan

Reputation: 60564

You could try the jQuery Form Plugin.

Upvotes: 0

Related Questions