dynamic checkbox selection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TheVillain9
    New Member
    • Sep 2006
    • 7

    dynamic checkbox selection

    I'm basically using AJAX and returning a bunch of information through XML and creating a checkbox. If the the XML returns that the checkbox should be set to true, check it, otherwise leave it empty. Below is my iteration when i go through the xml document. also i'd like to have the checkbox call a function when clicked, but I can't get it to work in both IE & Mozilla. Thanks for the help.

    Code:
    var chkbox = document.createElement('input');
    chkbox.type='checkbox';
    chkbox.setAttribute('name','chkPortfolio');
    chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
    chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
    if( portfolios.childNodes[j].attributes[1].text=="True") {
    	chkBox.onclick= function(evt) { refCell.set
    }
    else {
    	chkbox.checked=false;
    }
    chkbox.id = "chkPortfolio_" + (j+1);
    chkbox.li_id = "trPortfolio_" + (j+1);
    var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
    					listItem.appendChild(chkbox);
    					listItem.appendChild(name);
    					list.appendChild(listItem);
    }
  • b1randon
    Recognized Expert New Member
    • Dec 2006
    • 171

    #2
    Originally posted by TheVillain9
    I'm basically using AJAX and returning a bunch of information through XML and creating a checkbox. If the the XML returns that the checkbox should be set to true, check it, otherwise leave it empty. Below is my iteration when i go through the xml document. also i'd like to have the checkbox call a function when clicked, but I can't get it to work in both IE & Mozilla. Thanks for the help.

    Code:
    var chkbox = document.createElement('input');
    chkbox.type='checkbox';
    chkbox.setAttribute('name','chkPortfolio');
    chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
    chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
    if( portfolios.childNodes[j].attributes[1].text=="True") {
    	chkBox.onclick= function(evt) { refCell.set
    }
    else {
    	chkbox.checked=false;
    }
    chkbox.id = "chkPortfolio_" + (j+1);
    chkbox.li_id = "trPortfolio_" + (j+1);
    var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
    					listItem.appendChild(chkbox);
    					listItem.appendChild(name);
    					list.appendChild(listItem);
    }
    You had quite a few syntax problems but I ironed them out. This will work cross-browser:
    Code:
    <html>
    <head>
    <script>
    	function blah(){
    		var j=0;
    		var chkbox = document.createElement('input');
    		chkbox.type='checkbox';
    		chkbox.setAttribute('name','chkPortfolio');
    		chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
    		chkbox.setAttribute('value',"blah");
    		if( "True"=="True") {
    			chkbox.onclick= function() {alert('hi')};
    		}
    		else {
    			chkbox.checked=false;
    		}
    		chkbox.id = "chkPortfolio_" + (j+1);
    		chkbox.li_id = "trPortfolio_" + (j+1);
    		//var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
    		document.getElementById('body').appendChild(chkbox);
    		//listItem.appendChild(name);
    		//list.appendChild(listItem);
    	}
    </script>
    </head>
    <body id="body" onload="blah()">
    fdsfsdafasdfdsfsdasda
    </body>
    </html>

    Comment

    • TheVillain9
      New Member
      • Sep 2006
      • 7

      #3
      Originally posted by b1randon
      You had quite a few syntax problems but I ironed them out. This will work cross-browser:
      Code:
      <html>
      <head>
      <script>
      	function blah(){
      		var j=0;
      		var chkbox = document.createElement('input');
      		chkbox.type='checkbox';
      		chkbox.setAttribute('name','chkPortfolio');
      		chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
      		chkbox.setAttribute('value',"blah");
      		if( "True"=="True") {
      			chkbox.onclick= function() {alert('hi')};
      		}
      		else {
      			chkbox.checked=false;
      		}
      		chkbox.id = "chkPortfolio_" + (j+1);
      		chkbox.li_id = "trPortfolio_" + (j+1);
      		//var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
      		document.getElementById('body').appendChild(chkbox);
      		//listItem.appendChild(name);
      		//list.appendChild(listItem);
      	}
      </script>
      </head>
      <body id="body" onload="blah()">
      fdsfsdafasdfdsfsdasda
      </body>
      </html>
      sorry i actually copied the wrong thing, but this helps. only problem is when...

      Code:
      if( "True"=="True") {
      	chkbox.onclick= function() {alert('hi')};
      	[B]chkbox.checked=true;[/B]
      }
      i want to check the checkbox, but that doesn't seem to work. suggestions

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        Originally posted by TheVillain9
        sorry i actually copied the wrong thing, but this helps. only problem is when...

        Code:
        if( "True"=="True") {
        	chkbox.onclick= function() {alert('hi')};
        	[B]chkbox.checked=true;[/B]
        }
        i want to check the checkbox, but that doesn't seem to work. suggestions
        Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.

        Code:
        chkbox.checked=true;
        should work as long as chkbox is a valid reference to a checkbox.

        Comment

        • b1randon
          Recognized Expert New Member
          • Dec 2006
          • 171

          #5
          Originally posted by acoder
          Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.

          Code:
          chkbox.checked=true;
          should work as long as chkbox is a valid reference to a checkbox.
          That was my bad. I hard coded that condition because he was referencing some data that wasn't in his code snippet (and I couldn't debug). I'm sure when he uses the changes he'll put it back to the way it originally was.

          Comment

          • b1randon
            Recognized Expert New Member
            • Dec 2006
            • 171

            #6
            Originally posted by acoder
            Why are you checking if "True" equals "True"? That will always return true so the code is redundant. It also means that the checkbox will never be unchecked.

            Code:
            chkbox.checked=true;
            should work as long as chkbox is a valid reference to a checkbox.
            I can see the issue too. It works fine in FFox but IE doesn't show the check. I'm working on why. If anyone knows, do tell.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Originally posted by b1randon
              I can see the issue too. It works fine in FFox but IE doesn't show the check. I'm working on why. If anyone knows, do tell.
              Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.

              Comment

              • TheVillain9
                New Member
                • Sep 2006
                • 7

                #8
                Originally posted by acoder
                Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.
                well basically i have a div called 'portfolio_box' within my form, where i append all the checkboxes i create. so i don't understand why it doesn't work in IE(6.0.2900) or FF(2.0.0.1) for me.

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Then, in that case, post the entire code, or give a url to the page containing your code.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by acoder
                    Most likely, the reason is that the checkbox must be contained within a form. So it should be appended to the body, rather it should be child to a form tag/element.
                    Mistake: the second sentence should be, So it should not be appended to the body, rather it should be a child element to a form/tag element.

                    I would've edited the original post, but it's already been quoted upon (noticed it too late).

                    Comment

                    • TheVillain9
                      New Member
                      • Sep 2006
                      • 7

                      #11
                      Originally posted by acoder
                      Mistake: the second sentence should be, So it should not be appended to the body, rather it should be a child element to a form/tag element.

                      I would've edited the original post, but it's already been quoted upon (noticed it too late).
                      This is all within an aspx web page, but there is a whole bunch of other stuff going on, so i made a quick sample for a simple page. The checkboxes are selected in FF , but not IE6...any ideas? I can't get either to be checked when I essentially have this in my aspx page. Thanks for the help.

                      Code:
                      <html>
                      <header>
                      <script language="javascript"> 
                      	function loadPort() {
                      		
                      		var portObj = document.getElementById('portfolio_box');
                      		var list = document.createElement('ul');
                      		list.setAttribute('id','chk_list');
                      		portObj.appendChild(list);
                      		for(j=0; j<10; j++)
                      		{	var listItem = document.createElement('li');
                      			listItem.setAttribute('id',"trPortfolio_" + (j+1));
                      			var chkbox = document.createElement('input');
                      			chkbox.setAttribute('type','checkbox');
                      			chkbox.setAttribute('name',"nuts");
                      			chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
                      			chkbox.setAttribute('value',"nuts");
                      			if("True"=="True") {
                      				/* Nothing seems to work */
                      [B]				chkbox.checked=1;
                      				chkbox.checked=true;
                      				chkbox.setAttribute('selected',true);
                      				chkbox.setAttribute('selected','true');[/B]
                      			}
                      			else {
                      				chkbox.checked=false;
                      			}
                      			chkbox.onclick= function() { 
                      					alert("nuts");
                      				}
                      			chkbox.id = "chkPortfolio_" + (j+1);
                      			var name = document.createTextNode("test" + (j+1));
                      			listItem.appendChild(chkbox);
                      			listItem.appendChild(name);
                      			list.appendChild(listItem);
                      		}
                      		
                      		portObj.appendChild(list);
                      	}
                      </script>	
                      
                      </header>
                      
                      
                      <body>
                      
                      
                      <form id="Form1" method="post" >
                      			<div id="message"></div>
                      			<div class="item_list">
                      				<div class="title">Portfolios <input id="checkIt" onclick="checkAll(this.checked,'chkPortfolio',60);" type="checkbox" checked></div>
                      				<div id="portfolio_box"></div>
                      			</div>
                      			<input type="button" onclick="loadPort();" value="click me">
                      </form>
                      
                      </body>

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Ok, I figured it out.

                        IE, in its wisdom, stupidly ignores your checked setting until and unless you have appended the checkbox to something. If it's still free-standing, it just completely ignores it and sets it to (or keeps it) unchecked!

                        There are two ways around the problem. One is to set the checked property after you've appended the checkbox to the list-item. The second option is to use the defaultChecked property instead (thankfully, at least, IE recognises that!)

                        I got this info. from this site.

                        Comment

                        • vinod8427
                          New Member
                          • Sep 2010
                          • 1

                          #13
                          Thanks

                          Of all the research I did, I could find just the hack but none explained as to why it wouldnt work with IE. Thanks to Acoder for detailed explanation.

                          Comment

                          Working...