I have a script as below
document.getElementById('lan').innerHTML=lan;
document.getElementById('city').innerHTML=city;
document.getElementById('text1').value=city+'|'+lan;
The variables lan and city contain text, for example ‘kalmar’.
What I want to do is to take the contents of these two variables and combine them and set that combined string to be a form field’s value, as to be able to use the regular submit function of the form to save it to database.
In Chrome and FF it works perfectly, but not in IE (surprise).
If I do alert(document.getElementById('text1').value) to see what value it holds IE only prints
[object]
Any clues as to what has happened/what I can do to make it compatible?
It’s a big document but I cut and paste the essentials here:
HTML-element to receive the final combined value
<input type='hidden' name='text1' id='text1' value=''>
HTML-element (link) to assign value to above HTML element:
<div id="searchResult"><a href="javascript:populateFields('Kalmar','Kalmar');">Kalmar i Kalmar</a><br></div>
function populateFields(lan,city)
{
document.getElementById('lan').innerHTML=lan;
document.getElementById('city').innerHTML=city;
document.getElementById('text1').setAttribute('value',city+'|'+lan)
document.getElementById('save_button').style.zIndex='auto';
alert('LAN: '+document.getElementById('lan').innerHTML);
alert('CITY: '+document.getElementById('city').innerHTML);
alert('TEXT1: '+document.getElementById('text1').value);
}
Also – the alert() checks now print the correct values (i.e. Kalmar|Kalmar) but the form still doesn’t save teh value that I have set the text1 field to!
So I discovered there were a number of issues here.
First, the function populate_fields() had local variables named the same as some html elements in the document. IE does not like that, hence it printed [object] when I tried to read the value stored in my variables (since there was already an html element/DOM object with the same name, I guess my function variables just never declared).
Once that was sorted out, I also realized that the form didn’t save my value, though it seemed that the form item had received the correct value (I checked using alert).
Turns out that I had a nested form, I.e.
and that made IE totally disregard the last form field… for some reason none of the other browsers had a problem…
Thanks for the attention guys!