SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: Javascript loop

Hybrid View

  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

    Code JavaScript:
    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();
    	}
    }

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

    Code JavaScript:
    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();
        }
    }

  3. #3
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help, but it doesn't seem to like this line:

    namelist = namelist.split(',');

  4. #4
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,198
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Is there a demo page where i can see the script in action as it should work perfectly fine.

  5. #5
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    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?

    Code JavaScript:
    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();
        }
    }

  6. #6
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any ideas on this guys? I know it's a saturday night :-) but this has got me really frustrated!!

    Thanks for any help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •