Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery val() returns empty string

This is very puzzling and I am not even sure what code should I post here. Yet the problem is very simple. I have a form in a JQuery modal dialog where I am doing some price calculations. Among others, I have a field Discount. Presented as:

<div class="editor-label">
    @Html.LabelFor(model => model.Discount)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Discount, new { size = 10 })
</div>

I see in the Firebug debugger, DOM tab, that this field has a value of 20. And it is visible on the form, of course. But calling:

 $('#Discount').val()

returns an empty string. And this is the only field that has this problem. Calling val() for other fields returns correct values. Even clicking submit and debugging the MVC action for the model submitted, I see that the value for Discount is there. It is just the JQuery function that returns an empty string and only for this field.

I know this is a bit of an abstract question, but has anyone ever had an experience like this?

Thank you

Generated HTML:

<div class="editor-label">
  <label for="Discount">Popust (%)</label>
</div>
<div class="editor-field">
  <input id="Discount" class="text-box single-line valid" type="text" value="0" name="Discount" data-val-range-min="0" data-val-range-max="100" data-val-range="Vredonst mora biti izmedju 0 i 100" data-val-number="Polje Popust (%) mora biti broj" data-val="true" size="10">
</div>

While DOM tab in Firebug for this element shows value: "20"

like image 792
elector Avatar asked May 31 '12 08:05

elector


People also ask

What does val() return in jQuery?

The val() method returns or sets the value attribute of the selected elements. When used to return value: This method returns the value of the value attribute of the FIRST matched element.

What does. val do in JavaScript?

The val() method is an inbuilt method in jQuery which is used to returns or set the value of attribute for the selected elements. This method apply on the HTML form elements.

How can we use jQuery to Get the value of an input element with id name?

Answer: Use the jQuery val() Method You can simply use the jQuery val() method to get the value in an input text box. Try out the following example by entering something in the text input box and then click the "Show Value" button, it will display the result in an alert dialog box.


2 Answers

Check that id 'Discount' is unique.

When putting multiple forms on a single page I leave the 'name' alone but always prefix the id of the 'form' element to the id of each 'value'.

Saves me alot of headaches.

like image 135
Daren Schwenke Avatar answered Nov 10 '22 11:11

Daren Schwenke


Daren's answer definitely hit the nail on the head.

However, there are times when you want to return the value of multiple elements that have the same ID. (Note: IDs should be unique. Classes, on the other hand, don't need to be unique.)

To accomplish this you need to iterate with the .each() method. Most jQuery methods that return an object implicitly iterate. .val() returns a string, not an object. Because of that, it needs an explicit iteration loop.

You can do use the .each() method like this.

$('.discount').each(function(){
    $(this).val()
});

Please note that I changed the OP's ID to a class so that it is correct HTML.

like image 39
Daniel Morell Avatar answered Nov 10 '22 10:11

Daniel Morell