views:

41

answers:

4

$('#myhidde').attr("value",data[0]['MODE']);

Iam calling ajax page which returns me json data after getting the data from JSON results when i alert also iam able to see the value but iam not able to write to hidden field .What would be the reason .Dying for 2hrs

+3  A: 

You need to use val() for hidden field:

$('#myhidde').val(data[0]['MODE']);
Sarfraz
Gotta love SO, I was beaten twice within a minute...
Harmen
Any documentation on why this works then vs. setting the "value" attribute?
scunliffe
@sarfraz: I tried this approach also but did not work out
Someone
@Someone: Is your code within `ready` handler? Are you using the `form` tag? What is the error you get then?
Sarfraz
@Someone, the syntax is correct, so there must be something wrong with the `data` object or with the HTML code
Harmen
@HARMEN:if i try to write the same value in to a text field it is being written
Someone
A: 

Use the jQuery val method:

$('#myhidde').val(data[0]['MODE']);
Oded
@oded: I tried this approach also but did not work out
Someone
@Someone - what error are you getting in the javascript console?
Oded
@oded:if i try to write the same value in to a text field it is being written
Someone
+1  A: 

First check what is inside your data object: console.log( data );

Then, use the correct syntax:

$('#myhidde').val( data[0]['MODE'] );
Harmen
+1  A: 

I'm summing up all my comments in an answer here:

Be aware of the following when trying to debug hidden/input field interaction.

  1. View-Source in browsers shows you the "static" HTML that was part of the original HTTPResponse 1.1 If your browser supports it, try selecting text on the page around the element and choose "view selection source" this typically reveals "up-to-date" source
  2. If you try to use an onchange event handler on a field to alert the new value after you've set it programatically via JavaScript, it will NOT alert the value because the onchange event only fires when the user interacts with the field to change the value
  3. Firebug may not appear to update the value of a hidden field under special circumstances (I've witnessed this, but been unable to find a reliable test case to submit a bug) - get a 2nd verification from JavaScript or submitting the page
  4. Try changing the type attribute temporarily from "hidden" to "text" so that you can see the value when it changes
scunliffe