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

Dynamically create checkbox based on the output in the responseText from server

P: 82
Hi,
Here I have an array returned that is returned in responseText from the server. Based on the output that is returned by the server I have to dynamically create a checkbox.
Expand|Select|Wrap|Line Numbers
  1. function StateChanged() {
  2. if(xmlHttp.readyState == 4) {
  3. xyz=xmlHttp.responseText;
  4. if(xyz!="false") {
  5. alert("true");
  6. Here I have to add a function to create a checkbox 
  7. }
  8. }
  9. }
  10.  
Somebody please help me??
I was trying to use document.createElement() but it did not work.
Thanks
Jan 21 '09 #1
Share this Question
Share on Google+
37 Replies


Dormilich
Expert Mod 5K+
P: 8,639
you probably didn't append the created element to the document tree?
Jan 22 '09 #2

P: 82
now am able to create a checkbox if a button is clicked. I wanted to check whether that particular checkbox if clicked another javascript function has to be evoked, am not able to figure out how to do that...
Jan 22 '09 #3

Dormilich
Expert Mod 5K+
P: 8,639
something like
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(checkbox_id).addEventListener("click", callback_function, false);
  2. // "change" instead of "click" should work too
  3. // does not work in IE, workarounds available
Jan 22 '09 #4

P: 82
Is there a possibility to add a text next to the dynamically created checkbox
Jan 23 '09 #5

P: 82
And also onclick of the checkbox can we find is the checkbox been checked or unchecked
Jan 23 '09 #6

Dormilich
Expert Mod 5K+
P: 8,639
@sarega
yes
@sarega
yes

(for the sake of 20 chars)
Jan 23 '09 #7

P: 82
Can we pass arguments to the callback function in the eventlistener
Jan 23 '09 #8

Dormilich
Expert Mod 5K+
P: 8,639
not directly, you have three options:
  1. use a closure. the parameter must be known at closure creation time
    Expand|Select|Wrap|Line Numbers
    1. function parent_function(elem, param)
    2. {
    3.     var temp = function() { callback_with_param(param); }
    4.     elem.addEventListener("click", temp, false);
    5. }
  2. use a property of the used element
    Expand|Select|Wrap|Line Numbers
    1. function callback()
    2. {
    3.     var my_var = this.param;
    4.     // do cool stuff here using my_var
    5. }
    6.  
    7. function myFunc(elem, para);
    8. {
    9.     elem.param = para;
    10.     elem.addEventListener("click", callback, false);
    11. }
  3. use an anonymous function. this event can not be removed!
    Expand|Select|Wrap|Line Numbers
    1. elem.addEventListener("click", function() { yourFunc(param); }, false);
Jan 23 '09 #9

P: 82
I have dynamically added many checkboxes to the form and each of the checkboxes have different name,id and value. When the form is submitted how can I retrieve only those checkboxes which have been checked? The number of checkboxes in the form will vary everytime.
I will have something like this if i try to print all the posted values in the form
[red] => on [blue] => on
I want the red and blue, for my further processing
Jan 29 '09 #10

Dormilich
Expert Mod 5K+
P: 8,639
as far as I know, only the selected checkboxes will show up in the submitted data.

you can also filter off empty (i.e. all values that evaluate as false) values by
Expand|Select|Wrap|Line Numbers
  1. // PHP
  2. $array = array_filter($_POST);
Jan 29 '09 #11

P: 82
am trying to use both attachEvent and addEventListener but attchEvent is not working in IE
Expand|Select|Wrap|Line Numbers
  1. if(document.getElementById('chk').addEventListener) {
  2.                 document.getElementById('chk').addEventListener("click",function() { abc(colors); },false);
  3.         } else if(document.getElementById('chk').attachEvent) {
  4.                 alert("here");
  5.                 document.getElementById('chk').attachEvent('onClick', function() { abc(colors); });
  6.         } else {
  7.                 alert("Your browser does not support any handlers");
  8.         }
  9.  
Can somebody tell me what is the problem?
Jan 30 '09 #12

Dormilich
Expert Mod 5K+
P: 8,639
try (Javascript is case sensitive)
Expand|Select|Wrap|Line Numbers
  1. document.getElementById('chk').attachEvent('onclick', function() { abc(colors); });
otherwise you can use various cross browser functions ( e.g. addEvent() ) available via google
Jan 30 '09 #13

P: 82
I have tried onclick also but even that did not work
Jan 30 '09 #14

Dormilich
Expert Mod 5K+
P: 8,639
maybe you should then use one of the addEvent() functions, they're working.

possibly attachEvent() doesn't like the anonymous function...?
Jan 30 '09 #15

P: 82
attachEvent itself worked, thanks
does the
Expand|Select|Wrap|Line Numbers
  1. var div=document.getElementById("div_id");
  2. var example=document.createElement('input');
  3. example.type='submit';
  4. example.name='submit';
  5. example.value='submit;
  6. div.appendChild(example);
  7.  
not work in IE because its working in mozilla but not in IE.
Jan 30 '09 #16

Dormilich
Expert Mod 5K+
P: 8,639
I'm not sure if IE creates attributes via element.value = "value"; , you still could try the DOM methods setAttribute() or createAttribute() (at least they work for me)
Jan 30 '09 #17

acoder
Expert Mod 15k+
P: 16,027
You have an end quote missing on line 5. Always check the error console.
Jan 30 '09 #18

P: 82
actually the end quote is not the problem, I removed the element.value=value but still its not working
Jan 30 '09 #19

acoder
Expert Mod 15k+
P: 16,027
Well, it would've been a problem if it was missing. When you say it's not working, what exactly do you mean? Does it not show up? Does it not submit the form when clicked?
Jan 30 '09 #20

P: 82
It does not submit the form when clicked
Jan 30 '09 #21

acoder
Expert Mod 15k+
P: 16,027
Have you set the action of the form and is the div inside a form?
Jan 31 '09 #22

P: 82
yes I have, its working for mozilla but not in IE
Feb 1 '09 #23

acoder
Expert Mod 15k+
P: 16,027
Post the HTML code or at least some test code demonstrating the problem.
Feb 1 '09 #24

P: 82
Expand|Select|Wrap|Line Numbers
  1. var div=document.getElementById("div_id");
  2. var example=document.createElement('input');
  3. example.type='submit';
  4. example.name='submit';
  5. example.value='submit';
  6. div.appendChild(example);
  7.  
Feb 1 '09 #25

P: 82
Expand|Select|Wrap|Line Numbers
  1. var test=document.getElementById('mydiv');
  2.     var br=document.createElement('br');
  3.     test.appendChild(br);
  4.     var check=document.createElement('input');
  5.  
  6.     check.type='checkbox';
  7.     check.id='check';
  8.     check.name='Testmode';
  9.     check.value='on';
  10.  
  11.     var text='Testmode';
  12.     test.appendChild(check);
  13.     test.appendChild(document.createTextNode(text));    
  14.     var bre=document.createElement('br');
  15.     test.appendChild(bre);
  16.     var brea=document.createElement('br');
  17.  
  18.     test.appendChild(brea);
  19.     var submit=document.createElement('input');
  20.     submit.type='submit';
  21.     submit.name='submit';
  22.     submit.id='submit';
  23.  
  24.     submit.value='submit';
  25.     test.appendChild(submit);
  26.  
Feb 1 '09 #26

acoder
Expert Mod 15k+
P: 16,027
That's the JavaScript code. We already have that. What about the HTML code and where and how this JavaScript code is called? You could, of course, just post a link instead if possible.
Feb 1 '09 #27

P: 82
Am calling this function as soon as the response is returned form the server.
But not able to figure out why its not working in IE.
Feb 5 '09 #28

acoder
Expert Mod 15k+
P: 16,027
You still haven't posted the HTML code.
Feb 5 '09 #29

P: 82
Expand|Select|Wrap|Line Numbers
  1. <input id="chkbx" type="checkbox" value="on" name="example"/>
  2. Change the color?
  3. <br/>
  4. <br/>
  5. Colors
  6. <br/>
  7. <br/>
  8. <input id="red" type="checkbox" name="colors[]" value="red"/>
  9. red
  10. <br/>
  11. <input id="blue" type="checkbox" name="colors[]" value="blue"/>
  12. blue
  13. <br/>
  14. <input id="green" type="checkbox" name="colors[]" value="green"/>
  15. green
  16. <br/>
  17. </div>
  18. <br/>
  19. <input id="check" type="checkbox" name="Testmode" value="on"/>
  20. Testmode
  21. <br/>
  22. <br/>
  23. <input id="submit" type="submit" name="submit" value="submit"/>
  24. <br/>
  25.  
Feb 6 '09 #30

P: 82
I have figured out that the problem is due to the usage of two different div's can you tell me how to use insertbefore to satisfy my requirement??
the usage of one div the problem is that
->first there is a text box based on the value entered in the textbox and clicked ajax is used to get the response,based on the response
->if the response is true then some more checkboxes is created and then another dummy checkbox and then submit is created dynamically
->if the response is false then dummy checkbox and submit is created
If i do not use 2 div's then the checkboxes created when the response is true is appended after the dummy and submit button but actually it has to be created before the dummy and submit button
How do I achieve this with insertbefore??
Feb 6 '09 #31

acoder
Expert Mod 15k+
P: 16,027
Before I answer that, can you answer this: if you already have a submit button, why do you create another identical one?
Feb 7 '09 #32

P: 82
I do not have a submit button before am creating it dynamically later
Feb 8 '09 #33

acoder
Expert Mod 15k+
P: 16,027
Then what about the code you posted in #30?
Feb 9 '09 #34

P: 82
I create that dynamically after the response is returned from the ajax
Feb 9 '09 #35

acoder
Expert Mod 15k+
P: 16,027
OK, when I asked for the HTML code earlier, I meant the HTML code before any changes.

I think we're going round in circles, so the best thing would be for you to post a link to your site, so that we can see all of the code.
Feb 9 '09 #36

P: 82
Sorry that cannot be done because its confidential...
what do you want me to post?
Feb 9 '09 #37

acoder
Expert Mod 15k+
P: 16,027
Either PM me the link or attach the files here (removing sensitive/confidential info).
Feb 9 '09 #38

Post your reply

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