-3

Code:

$(".drpdwn")[0].options.value = "1"

I need to change this value to "string"

So, I coded as,

$(".drpdwn")[0].options.value = "string"

But, the value becomes as "" in quickwatch.

How to apply string value?

John Stephen
  • 1,961
  • 3
  • 24
  • 47
  • possible duplicate of [Set value of textarea in jQuery](http://stackoverflow.com/questions/415602/set-value-of-textarea-in-jquery) – Imad Alazani Jun 27 '13 at 08:43

2 Answers2

5

$('.drpdwn').val('string');

See the docs.

Edit:

As there is some confusion about this. $('.drpdwn') is a jQuery object representing all elements with the drpdwn class. This jQuery object has a val method. If you select only one of the matched objects using $('.drpdwn')[0] you get a DOM object (pretty much what you would get if you did document.getElementsByClassName('drpdwn')[0]). This obviously has no val method.

The better way of going around this, is making $('.drpdwn') more precise, so that it only matches the element you want to change. The easiest way is by giving your element an id (ala <div id="myElement" class="drpdwn"></div>) and selecting it with $('#myElement'). The other way would be by using the value property of the DOM object, like $('.drpdwn')[0].value. This will however break as soon as you insert an other element with that class before the element you want to change.

Sumurai8
  • 19,483
  • 10
  • 67
  • 96
2

using jQuery,

$(".drpdwn").val("string");

I think you're trying in JS. if so

document.getElementsByClassName("drpdwn").value="string";
Praveen
  • 53,079
  • 32
  • 129
  • 156