By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

dynamic checkbox selection

P: 7
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.

Expand|Select|Wrap|Line Numbers
  1. var chkbox = document.createElement('input');
  2. chkbox.type='checkbox';
  3. chkbox.setAttribute('name','chkPortfolio');
  4. chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
  5. chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
  6. if( portfolios.childNodes[j].attributes[1].text=="True") {
  7.     chkBox.onclick= function(evt) { refCell.set
  8. }
  9. else {
  10.     chkbox.checked=false;
  11. }
  12. chkbox.id = "chkPortfolio_" + (j+1);
  13. chkbox.li_id = "trPortfolio_" + (j+1);
  14. var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
  15.                     listItem.appendChild(chkbox);
  16.                     listItem.appendChild(name);
  17.                     list.appendChild(listItem);
  18. }
Jan 29 '07 #1
Share this Question
Share on Google+
12 Replies


b1randon
Expert 100+
P: 171
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.

Expand|Select|Wrap|Line Numbers
  1. var chkbox = document.createElement('input');
  2. chkbox.type='checkbox';
  3. chkbox.setAttribute('name','chkPortfolio');
  4. chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
  5. chkbox.setAttribute('value',portfolios.childNodes[j].firstChild.data);
  6. if( portfolios.childNodes[j].attributes[1].text=="True") {
  7.     chkBox.onclick= function(evt) { refCell.set
  8. }
  9. else {
  10.     chkbox.checked=false;
  11. }
  12. chkbox.id = "chkPortfolio_" + (j+1);
  13. chkbox.li_id = "trPortfolio_" + (j+1);
  14. var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
  15.                     listItem.appendChild(chkbox);
  16.                     listItem.appendChild(name);
  17.                     list.appendChild(listItem);
  18. }
You had quite a few syntax problems but I ironed them out. This will work cross-browser:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4.     function blah(){
  5.         var j=0;
  6.         var chkbox = document.createElement('input');
  7.         chkbox.type='checkbox';
  8.         chkbox.setAttribute('name','chkPortfolio');
  9.         chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
  10.         chkbox.setAttribute('value',"blah");
  11.         if( "True"=="True") {
  12.             chkbox.onclick= function() {alert('hi')};
  13.         }
  14.         else {
  15.             chkbox.checked=false;
  16.         }
  17.         chkbox.id = "chkPortfolio_" + (j+1);
  18.         chkbox.li_id = "trPortfolio_" + (j+1);
  19.         //var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
  20.         document.getElementById('body').appendChild(chkbox);
  21.         //listItem.appendChild(name);
  22.         //list.appendChild(listItem);
  23.     }
  24. </script>
  25. </head>
  26. <body id="body" onload="blah()">
  27. fdsfsdafasdfdsfsdasda
  28. </body>
  29. </html>
  30.  
Jan 29 '07 #2

P: 7
You had quite a few syntax problems but I ironed them out. This will work cross-browser:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script>
  4.     function blah(){
  5.         var j=0;
  6.         var chkbox = document.createElement('input');
  7.         chkbox.type='checkbox';
  8.         chkbox.setAttribute('name','chkPortfolio');
  9.         chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
  10.         chkbox.setAttribute('value',"blah");
  11.         if( "True"=="True") {
  12.             chkbox.onclick= function() {alert('hi')};
  13.         }
  14.         else {
  15.             chkbox.checked=false;
  16.         }
  17.         chkbox.id = "chkPortfolio_" + (j+1);
  18.         chkbox.li_id = "trPortfolio_" + (j+1);
  19.         //var name = document.createTextNode(portfolios.childNodes[j].firstChild.data);
  20.         document.getElementById('body').appendChild(chkbox);
  21.         //listItem.appendChild(name);
  22.         //list.appendChild(listItem);
  23.     }
  24. </script>
  25. </head>
  26. <body id="body" onload="blah()">
  27. fdsfsdafasdfdsfsdasda
  28. </body>
  29. </html>
  30.  
sorry i actually copied the wrong thing, but this helps. only problem is when...

Expand|Select|Wrap|Line Numbers
  1. if( "True"=="True") {
  2.     chkbox.onclick= function() {alert('hi')};
  3.     chkbox.checked=true;
  4. }
i want to check the checkbox, but that doesn't seem to work. suggestions
Jan 29 '07 #3

acoder
Expert Mod 15k+
P: 16,027
sorry i actually copied the wrong thing, but this helps. only problem is when...

Expand|Select|Wrap|Line Numbers
  1. if( "True"=="True") {
  2.     chkbox.onclick= function() {alert('hi')};
  3.     chkbox.checked=true;
  4. }
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.

Expand|Select|Wrap|Line Numbers
  1. chkbox.checked=true;
should work as long as chkbox is a valid reference to a checkbox.
Jan 29 '07 #4

b1randon
Expert 100+
P: 171
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 29 '07 #5

b1randon
Expert 100+
P: 171
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.

Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 29 '07 #6

acoder
Expert Mod 15k+
P: 16,027
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.
Jan 30 '07 #7

P: 7
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.
Jan 30 '07 #8

acoder
Expert Mod 15k+
P: 16,027
Then, in that case, post the entire code, or give a url to the page containing your code.
Jan 30 '07 #9

acoder
Expert Mod 15k+
P: 16,027
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).
Jan 30 '07 #10

P: 7
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.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <header>
  3. <script language="javascript"> 
  4.     function loadPort() {
  5.  
  6.         var portObj = document.getElementById('portfolio_box');
  7.         var list = document.createElement('ul');
  8.         list.setAttribute('id','chk_list');
  9.         portObj.appendChild(list);
  10.         for(j=0; j<10; j++)
  11.         {    var listItem = document.createElement('li');
  12.             listItem.setAttribute('id',"trPortfolio_" + (j+1));
  13.             var chkbox = document.createElement('input');
  14.             chkbox.setAttribute('type','checkbox');
  15.             chkbox.setAttribute('name',"nuts");
  16.             chkbox.setAttribute('id',"chkPortfolio_" + (j+1));
  17.             chkbox.setAttribute('value',"nuts");
  18.             if("True"=="True") {
  19.                 /* Nothing seems to work */
  20.                 chkbox.checked=1;
  21.                 chkbox.checked=true;
  22.                 chkbox.setAttribute('selected',true);
  23.                 chkbox.setAttribute('selected','true');
  24.             }
  25.             else {
  26.                 chkbox.checked=false;
  27.             }
  28.             chkbox.onclick= function() { 
  29.                     alert("nuts");
  30.                 }
  31.             chkbox.id = "chkPortfolio_" + (j+1);
  32.             var name = document.createTextNode("test" + (j+1));
  33.             listItem.appendChild(chkbox);
  34.             listItem.appendChild(name);
  35.             list.appendChild(listItem);
  36.         }
  37.  
  38.         portObj.appendChild(list);
  39.     }
  40. </script>    
  41.  
  42. </header>
  43.  
  44.  
  45. <body>
  46.  
  47.  
  48. <form id="Form1" method="post" >
  49.             <div id="message"></div>
  50.             <div class="item_list">
  51.                 <div class="title">Portfolios <input id="checkIt" onclick="checkAll(this.checked,'chkPortfolio',60);" type="checkbox" checked></div>
  52.                 <div id="portfolio_box"></div>
  53.             </div>
  54.             <input type="button" onclick="loadPort();" value="click me">
  55. </form>
  56.  
  57. </body>
Jan 31 '07 #11

acoder
Expert Mod 15k+
P: 16,027
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.
Feb 1 '07 #12

P: 1
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.
Sep 7 '10 #13

Post your reply

Sign in to post your reply or Sign up for a free account.