2

Here is a simple chunk of code:

1  var selected_clone = selected.clone();
2  alert(selected_clone.text());
3  var new_value = selected_clone.text();
4  form_li.find('input.ajax_selection').val(new_value);

Now.

Line 2 (debugging) outputs exactly the value I expect it to. Line 3 is redundant, I know, but I used it for testing: when passing an arbitrary string to new_value, the val in line 4 works perfectly. It does not change the value if I assign it the result of selected_clone.text()

The question is: why does it behave in such a puzzling way?

From chrome's debugging console, just chilling silence.


Additional info:

  • typeof(new_value) is string;
  • the value in the form field is actually updated: the value attribute, however is not.
  • about the latter point: no, it's not my debugger. the values sent on submit are non-updated.
  • the request sends the unupdated value; while the form displays the updated one.

Holy Shitzu this is weird.

I solved the problem by changing line 4 to: form_li.find('input.ajax_selection').attr('value',new_value);.

That does not make the slightest amount of sense to me though, and I would still like to know why.

cbrandolino
  • 5,873
  • 2
  • 19
  • 27
  • does "typeof new_value" return 'string' ? – Lepidosteus May 27 '11 at 14:35
  • Another blind try, what do you get with $selection.val(new_value + '') ? And with $selection.val(new_value + 'testtesttest') ? If neither of these work, could you post the content of new_value in your question (as returned by console.log() after line 3) -- EDIT: seeing the details you just added, are you using jQuery 1.6 ? And what type of input is it, text ? – Lepidosteus May 27 '11 at 14:39
  • All that ".val()" is *supposed* to do is update the "value" - what do you mean by, "the val() attribute is not" updated? You should check either the Chrome network tool to look at the *actual* HTTP request. – Pointy May 27 '11 at 14:41
  • Reverse the order of your operations. That is to say, assign `new_value` first, alert `new_value`, then assign. Make sure you're assigning what you think you are. – Brad Christie May 27 '11 at 14:42
  • What happens if you do `val(String(new_value))`? – Jamie Treworgy May 27 '11 at 14:45
  • @Pointy: sorry. I meant the `val=` html attribute. In the form, the new value is correctly displayed. @Brad: tried it; same results. I'm posting just a small chunk of code; my debugging has been herculean. @Lepidosteus, using 1.5.2 and input is text - I see good news coming and I think I'm gonna be grateful to you for the rest of my days. – cbrandolino May 27 '11 at 14:45
  • There is no legal "val" html attribute, do you mean the `value` attribute? – Jamie Treworgy May 27 '11 at 14:46
  • yep jamietre, sorry again. as for your previous question; nothing different. it *is* a string, I swear! – cbrandolino May 27 '11 at 14:48
  • Could you post a working reproduction of the error? – recursive May 27 '11 at 14:50
  • @cbrandolino what the heck is the "val" attribute? There's no such thing - the important attribute for form submission is definitely, unequivocally "value", not "val". The jQuery ".val()" method sets the "value" attribute, and **only** the "value" attribute. – Pointy May 27 '11 at 14:52
  • @Pointy, I'm talking about the "value" attribute. it was a typo. i apologised with jamietre already. – cbrandolino May 27 '11 at 14:55
  • @Pointy, check out the edit. things get weirder and weirder. – cbrandolino May 27 '11 at 15:13

2 Answers2

2

The answer to your new question can be found in that question: .prop() vs .attr()

To understand you will need to know that val() is a shortcut to prop('value')

Community
  • 1
  • 1
Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
  • Thanks! My, that's so great! It was really bugging me. – cbrandolino May 27 '11 at 15:27
  • As a still confused but curious bystander, I still don't understand why this would make val() fail. Shouldn't prop('value') always behave as expected? – recursive May 27 '11 at 18:05
  • If I understood the problem correctly, val() was working, but OP was checking the value attribute in the dom inspector and this one wasn't updated, which is normal. Try to enter some text in an input field and you will see that the value attribute is not updated in your browser's dom inspector; what is updated is the property of the dom object. So in short it worked, but OP was checking the wrong thing to confirm the change. – Lepidosteus May 27 '11 at 18:23
0

I would first try to know how many objects the form_li.find(...) method returns. Then I would check if

form_li.find('input.ajax_selection').attr("value",new_value)

works or not. Difficult to answer with so few code, sorry

Grooveek
  • 10,046
  • 1
  • 27
  • 37
  • That code was added because the problem is there. Srsly. If you read carefully the question, in particular around the "when passing an arbitrary string to new_value, the val in line 4 works perfectly" you'll notice how your case has been toroughly considered. Thanks anyway! – cbrandolino May 27 '11 at 14:51