eagle
eagle

Reputation: 99

Issues getting value from hidden input field with jQuery

$(':checkbox').change(function() {
   var targetName = $(this).attr('name');
   var target = $(document).find(':hidden[name=targetName]').val();
   alert(target);
});

I want to get the value of the hidden input with the same name as the checkbox (on change) My issue here is that the alert only results in "undefined", but if I alert the part without "val()" it gives me an object back. All my jQuery code is inside

$(document).ready(function(){});

Also I tired to put my script code under my html, but without no effect.

Upvotes: 1

Views: 84

Answers (1)

antyrat
antyrat

Reputation: 27765

You need to concatenate your variable and add double quotes:

var target = $(document).find(':hidden[name="'+ targetName + '"]').val();

Upvotes: 3

Related Questions