-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?

Earth
  • 3,477
  • 6
  • 37
  • 78
  • 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
  • 20,333
  • 11
  • 66
  • 100
  • If I am changing any integer value in quickwatch..say for eg., "1" to "3" means it accepts. Why this happens ? – Earth Jun 27 '13 at 08:48
  • It's because the address is same and value is made different – Imad Alazani Jun 27 '13 at 08:50
  • Also, I am getting the error message as `Object doesn't support property or method 'val'` when I coded as follows `$(".drpdwn")[0].options.val('string');` – Earth Jun 27 '13 at 08:51
  • @JohnStephen `$(".drpdwn").val('string');` try this or `document.getElementsByClassName("drpdwn").value="string";`. – Praveen Jun 27 '13 at 08:54
  • @JohnStephen I've added some more information about the difference between `$('x')` and `$('x')[0]` – Sumurai8 Jun 27 '13 at 09:23
2

using jQuery,

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

I think you're trying in JS. if so

document.getElementsByClassName("drpdwn").value="string";
Praveen
  • 55,303
  • 33
  • 133
  • 164