0

I am trying to retrieve a value from a hidden field. This code worked fine with jquery 1.4.2 but since we upgraded to 1.6.1 or 1.6.4 it is just returning an empty string.

Here is the code in the jsp that is being used to retrieve the data:

var selected = "Doing My Job"   
var selectedField= "input:hidden[name="+selected+"_node_id]";
var selectedId= $(selectedField).val();

This is the html of the element itself

<input type="hidden" value="2" name="Doing My Job_node_id">

So I want selectedId to be 2 but instead I am getting ""

Any help would be greatly appreciated.

3
  • Are you sure the value of selected is bringing back the correct value? Try putting the output into an alert just to make sure. Commented Sep 19, 2011 at 14:01
  • Cannot reproduce: jsfiddle.net/pjdJH Just to be sure, you should put the attribute into quotes (they are mandatory). Commented Sep 19, 2011 at 14:01
  • I have alerted the value of selected and I am getting the correct value, again this worked in 1.4.2 but tried with 1.6.1 and 1.6.4 and it doesn't. @Felix Kling, cant understand how it is working for you, I tried your link and it works for me in there too
    – Kaskade
    Commented Sep 19, 2011 at 14:07

1 Answer 1

3

In theory, the name should be quoted:

var selectedField= 'input:hidden[name="'+selected+'_node_id"]';

Edit: Whatever, this doesn't seem to affect the final result. You should use your browser's development tools to inspect the contents of both the selectedField selector and the $(selectedField) object, e.g.:

console.log(selectedField, $(selectedField));
3
  • You are right, but the code seems to work without them as well: jsfiddle.net/pjdJH Commented Sep 19, 2011 at 14:04
  • changing to put in the quotes worked staright away, dont understand why it worked in Felix Klings link though
    – Kaskade
    Commented Sep 19, 2011 at 14:20
  • It seems to be hit and miss as far as when it works and doesn't work with and without the quotes around the attribute value. It has always had the quotes around the attribute value in the API, I would just suggest always having the quotes around the attribute value to prevent having issues in the future.
    – Kevin B
    Commented Sep 19, 2011 at 15:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.