View Full Version : Javascript if statement not executing?

03-26-2009, 08:10 PM
Hello, I have the following function that is being called onClick from a button.

function saveLocation(name, description, result, locationselect)
var resultElement = document.getElementById(result);

var nameText = document.getElementById(name).value;

var descText = document.getElementById(description).value;
var locationselectelement = document.getElementById(locationselect);

new Ajax.Request("ajax.php?operation=savelocation&location="+nameText+"&description="+descText,
method: 'post',
onSuccess: function(transport)
resultElement.innerHTML = "Successfully saved1.";

//refresh elements in pulldown

//find newly inserted element and select it
var i;
for(i = 0; i<locationselectelement.length; i++){
var OptionText = locationselectelement.options[i].innerHTML;


if(OptionText == nameText){
alert(OptionText+" was found when i="+i);

locationselectelement.selectedIndex = i;





Notice the

line. If I un-comment this line, the code works and the if statement is executed, but if I leave it commented (which I need to in the end), the if statement is not being executed. I will also remove the alert that is inside the if statement when I am done.

All of the variables are being initialized properly, because when I un-comment that line, the code works fine. Am I missing something?

Thanks for your help in advance.