TSCAmerica.com
TSCAmerica.com

Reputation: 5377

Accessing Hidden Field Value in Jquery

My form looks like the following

<form class="search_results_section" method="post" name="MainForm" id="MainForm" action="/searchresults.asp" onsubmit="return OnSubmitSearchForm(event, this);">
            <input type="hidden" name="Search" value="">
            <input type="hidden" name="Cat" value="1856">
</form>

I want to get the "Cat" value of Hidden field in a Variable

I tried using the following but not sure why its not working

  var elements =$('#jmenuhide input[name^="Cat"]').val();
      alert(elements);
  var ele=document.MainForm.getElementsByName('Cat').value;
      alert(ele);

The alert says "undefined"

Upvotes: 0

Views: 1705

Answers (4)

artlung
artlung

Reputation: 34013

For that markup, all you need is:

var cat = $('input[name="Cat"]').val();
var search = $('input[name="Search"]').val();

alert(cat); // 1856
alert(search); // (empty string)
​

Upvotes: 0

Musa
Musa

Reputation: 97672

You can get elements in a form by their name also what is #jmenuhide have you tried using #MainForm instead

var elements =$('#MainForm input[name="Cat"]').val();
  alert(elements);
var ele=document.MainForm.Cat.value;
  alert(ele);​

http://jsfiddle.net/N82s5/

Upvotes: 1

COLD TOLD
COLD TOLD

Reputation: 13579

just try using this not sure why you need this #jmenuhide

var elements =$('input[name^="Cat"]').val();

Upvotes: 1

gopi1410
gopi1410

Reputation: 6625

var elements =$('input[name="Cat"]').val();
      alert(elements);
var ele=document.getElementsByName('Cat')[0].value;
      alert(ele);

Here's the working fiddle: http://jsfiddle.net/gopi1410/NbCNu/

Upvotes: 1

Related Questions