View Full Version : Trouble accessing a form element

07-05-2012, 03:32 AM
I'm having trouble accessing a form element
I need to have a hidden field the value of which I need to pass through
I've seen lots of sample code, but just can't get it to work for what I need

I have some code that I adapted
I have:

var $contactform = $(this).closest('.contact-form-shop');

A few lines down I have:

var customername = $contactform.find('input[name="customername"]').val();
var email = $contactform.find('input[name="email"]').val();
var extradetails = $contactform.find('textarea[name="extradetails"]').val();

The above work fine!

I've added the following field:

<input type="hidden" name="shopName" value="XyZ" style="display: none;">

And I've tried things like:

var shopName = $contactform.find('shopName').val();

Console.log says the variable is undefined

Where am I going wrong?



07-05-2012, 04:01 AM
var shopName = window.document.getElementsByName('shopName')[0].value;
Or give it an ID;
var shopName = window.document.getElementById('shopName').value;

It looks like the code you have is for some library maybe you aren't using.

07-06-2012, 01:33 AM
thanks. that works a treat