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?
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);
}
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.
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...
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.