Injecting values into a form with javascript

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Injecting values into a form with javascript

Post 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);
		
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

How are you using it?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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().
(#10850)
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post 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...
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post 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);
Post Reply