-1

I have a situation where these 3 things are simultaneously true. What's going on?

This is the element in the dom:

<input id="destroy_vehicle_section_4" name="driver[workables_attributes][0][_destroy]" type="text" value="1">

After changing the attribute value with JQuery's val method (or javascript's getElementById().value), using the console:

> $("#destroy_vehicle_section_4").val()
<- "test"

...The DOM remains the same.

I can also change the value with JQuery's attr method,

> $("#destroy_vehicle_section_4").attr("value")
<- "hey"

The DOM remains the same, and the 2nd call also still show "test"

How is this possible?

It's meaning that my attempts to change the input using jQuery are failing.


N.B: Am using jQuery Mobile.

EDIT:

Sorry for the poor explanation. I first set using attr("value", "hey") and val("test"), neither console call changed the DOM element. However if you run the "Getter" lines in the console, you get the above

Will Taylor
  • 1,994
  • 2
  • 23
  • 36
  • 1
    *`After changing the attribute value with JQuery's val method`* you're not *changing* it, you're getting it. *`3 things are simultaneously true`* what? – Roko C. Buljan Mar 29 '15 at 23:40
  • Looks like a duplicate of http://stackoverflow.com/questions/8312820/jquery-val-vs-attrvalue or http://stackoverflow.com/questions/4837133/whats-the-difference-between-jquery-val-and-attrvalue – bvaughn Mar 29 '15 at 23:42
  • To *get* (retrieve) a value you use jQuery's `.val()` To *set* a value you need to pass a variable to the argument `.val("hey")` – Roko C. Buljan Mar 29 '15 at 23:44
  • So where do "test" and "hey" come from? – Ali Sheikhpour Mar 29 '15 at 23:46

2 Answers2

1

To get a value you use jQuery's .val()
To set a value you need to pass a variable to the argument .val("hey")

To get an attribute value you use jQuery's .attr("value")
To set an attribute value you need to set the second argument as .attr("value", "hey")

The above applies also for .prop() method.
How to properly use each of those methods I suggest you to refer to the official jQuery documentation.

Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
  • Sorry for the poor explanation. I set using attr("value", "hey") and val("test"), neither console call changed the DOM element. – Will Taylor Mar 30 '15 at 00:07
0

Try executing your functions in an event like onclick, onload or inside a document.ready() or simply

$(function(){

//your functions here...

});
Jethro Monzada
  • 104
  • 1
  • 9
  • Usually they are fired in a document.ready() - and it's not working then - but for demonstration I used the console. – Will Taylor Mar 31 '15 at 09:20