Javascript loop

Hi Guys!

This should be a very easy question, I know how to do it in PHP, but how can I do it in JavaScript…

I have a Javascript function which loops through a list of checkboxes and creates a string of the values of the selected checkboxes. However, it always adds an extra comma on the end of the string. How can I stop it adding the extra comma once it reaches the end of the loop?

function save_industries(){
	var namelist = "";
	with(document.select_industries) {
		for(var i = 0; i < industry.length; i++){
			if(industry[i].checked) {
				namelist += industry[i].value + ",";
			}
		}
	}
	if(namelist == "") {
		alert("Please select at least 1 industry.");
	} else {
		opener.document.searchform.industry.value = namelist;
		opener.document.getElementById("industry_list").innerHTML = "<span class=\\"info\\"><a href=\\"#\\" onclick=\\"javascript:select_industries('" + namelist + "')\\">Industries selected, click to edit</a></span>";
		window.close();
	}
}

I have changed your code so now it pushes the values into an array which then uses the javascript version of explode called split to return the values as a string. See how the below code goes for you.

function save_industries() {
    var namelist = [];
    
    with (document.select_industries) {
        for (var i = 0; i < industry.length; i++) {
            if (industry[i].checked) {
                namelist.push(industry[i].value);
            }
        }
    }
    
    if (!namelist.length) {
        alert('Please select at least 1 industry.');
    } else {
        namelist = namelist.split(',');
        opener.document.searchform.industry.value = namelist;
        opener.document.getElementById('industry_list').innerHTML = "<span class=\\"info\\"><a href=\\"#\\" onclick=\\"javascript:select_industries('" + namelist + "')\\">Industries selected, click to edit</a></span>";
        window.close();
    }
}

Thanks for your help, but it doesn’t seem to like this line:

namelist = namelist.split(‘,’);

Is there a demo page where i can see the script in action as it should work perfectly fine.

Great, I got it working thanks. Im now trying to get the script to save hidden fields to the parent window. I am trying to use jQuery to do this, but unfortunately my script doesn’t work. Any ideas what is wrong?


function save_industries() {
    var namelist = [];
    with(document.select_industries) {
        for(var i = 0; i < industry.length; i++) {
            if(industry[i].checked) {
				$('#opener.document.searchform').html('<input type="hidden" name="industry[]" value="' + industry[i].value + '">'); 
            }
        }
    }
    if(!namelist.length) {
        alert('Please select at least 1 industry.');
    } else {
        opener.document.getElementById('industry_list').innerHTML = "<span class=\\"info\\"><a href=\\"#\\" onclick=\\"java_script:select_industries('" + namelist + "')\\">Industries selected, click to edit</a></span>";
		window.close();
    }
}

Any ideas on this guys? I know it’s a saturday night :slight_smile: but this has got me really frustrated!!

Thanks for any help.