Friday, December 11, 2009

This javascript works in Firefox but not IE8 or Chrome?

Can someone tell me why this would work fine in Firefox 3.5.2 but not in IE 8 or Chrome 2? The value of the hidden field ';hours_quantity'; is not changed in the latter two browsers.





%26lt;select onchange=';javascript:{


x=document.getElementById('hours_quant鈥?br>

x.value=this.value;


}';%26gt;


%26lt;option selected=true value=';1';%26gt;1 hour%26lt;/option%26gt;


%26lt;option value=';2';%26gt;2 hours%26lt;/option%26gt;


%26lt;option value=';3';%26gt;3 hours%26lt;/option%26gt;


%26lt;option value=';4';%26gt;4 hours%26lt;/option%26gt;


%26lt;option value=';5';%26gt;5 hours%26lt;/option%26gt;


%26lt;/select%26gt;This javascript works in Firefox but not IE8 or Chrome?
What kind of element has the id ';hours_quantity'; ???








Addition Details:





In order to use function getElementById() you should first have an element with the specific id, in your case text box, then create an object points to that element.





Function getElementById() never creates elements; it just likes objects with elements.





If you are intending to change text box value with the selected value of a drop down menu then you need to create text box with id = ';hours_quantity';.





%26lt;select onchange=';javascript:{


var x=document.getElementById


('hours_quantity');


x.value=this.value;


}';%26gt;


%26lt;option selected=true value=';1';%26gt;1 hour%26lt;/option%26gt;


%26lt;option value=';2';%26gt;2 hours%26lt;/option%26gt;


%26lt;option value=';3';%26gt;3 hours%26lt;/option%26gt;


%26lt;option value=';4';%26gt;4 hours%26lt;/option%26gt;


%26lt;option value=';5';%26gt;5 hours%26lt;/option%26gt;


%26lt;/select%26gt;


%26lt;input id=';hours_quantity'; /%26gt;








Otherwise, you code with the following approach won't work neither in FireFox nor IE.





%26lt;select onchange=';javascript:{


var x=document.getElementById


('hours_quantity');


x.value=this.value;


}';%26gt;


%26lt;option selected=true value=';1';%26gt;1 hour%26lt;/option%26gt;


%26lt;option value=';2';%26gt;2 hours%26lt;/option%26gt;


%26lt;option value=';3';%26gt;3 hours%26lt;/option%26gt;


%26lt;option value=';4';%26gt;4 hours%26lt;/option%26gt;


%26lt;option value=';5';%26gt;5 hours%26lt;/option%26gt;


%26lt;/select%26gt;This javascript works in Firefox but not IE8 or Chrome?
The truncated line above looks like this:





x=document.getElementById


('hours_quantity');








You need to make it:


x=document.getElementById


('hours_quantity').value;





And it should work. No guarantees thought because I don't have the code for #hours_quanity.





Another problem could be your multiple declarations, first you tell X to be


document.getElementById


('hours_quantity').value;


And then you tell X to be


this.value.





You need to make it one or the other.
  • medium length hair
  • No comments:

    Post a Comment

     
    virus protection