Page 1 of 1

Injecting values into a form with javascript

Posted: Wed Oct 18, 2006 12:16 am
by Luke
I need to use javascript to inject some values into a form so that they are submitted when the use clicks "submit". This is what I have... what am I doing wrong?

Code: Select all

function updateForm(form, point){
	
	var lat = document.createElement('hidden');
	lat.value = point.lat();
	form.appendChild(lat);
	
	var lng = document.createElement('hidden');
	lng.value = point.lng();
	form.appendChild(lng);
		
}

Posted: Wed Oct 18, 2006 1:07 am
by Chris Corbyn
How are you using it?

Posted: Wed Oct 18, 2006 1:31 am
by Luke
It's part of a google maps application that I am trying desperately to wrap up (taking FAR too long). Basically, the form updates a map as you type in the address. (onchange="Map.locateAddress(this.form)") Once it plots the marker on the map, if google's geocoder couldn't find the address or it plotted it in the wrong spot, you can drag the marker to the location manually. The marker has an eventhandler that calles this function when you finish dragging the marker... I'd like that function to be able to insert the correct hidden form elements so that they are posted with the rest of the information in the form for insertion into the database. Right now I just have the hidden elements hard-coded into the form.

Posted: Wed Oct 18, 2006 1:43 am
by Christopher
I think you need to set lat.name and lng.name. Also if appendChild() is not working you may need to use nsertBefore().

Posted: Wed Oct 18, 2006 3:06 am
by nickvd
arborint wrote:I think you need to set lat.name and lng.name. Also if appendChild() is not working you may need to use nsertBefore().
Lack of the name attribute is probably the cause.. It took me a while before I caught that one, as you use the id to identify the element via js, but the name is needed for the form elements...

Posted: Wed Oct 18, 2006 3:23 am
by choppsta
I think it could be because you are trying to create a hidden element, when what you really want is an input element with the type attribute of hidden.

Code: Select all

var lat = document.createElement('input');
lat.setAttribute('type', 'hidden');
lat.setAttribute('value', point.lat());
form.appendChild(lat);