473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
37 3302
Dormilich
8,658 Expert Mod 8TB
you probably didn't append the created element to the document tree?
Jan 22 '09 #2
sarega
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
8,658 Expert Mod 8TB
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
sarega
82
Is there a possibility to add a text next to the dynamically created checkbox
Jan 23 '09 #5
sarega
82
And also onclick of the checkbox can we find is the checkbox been checked or unchecked
Jan 23 '09 #6
Dormilich
8,658 Expert Mod 8TB
@sarega
yes
@sarega
yes

(for the sake of 20 chars)
Jan 23 '09 #7
sarega
82
Can we pass arguments to the callback function in the eventlistener
Jan 23 '09 #8
Dormilich
8,658 Expert Mod 8TB
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
sarega
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
8,658 Expert Mod 8TB
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
sarega
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
8,658 Expert Mod 8TB
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
sarega
82
I have tried onclick also but even that did not work
Jan 30 '09 #14
Dormilich
8,658 Expert Mod 8TB
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
sarega
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
8,658 Expert Mod 8TB
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
16,027 Expert Mod 8TB
You have an end quote missing on line 5. Always check the error console.
Jan 30 '09 #18
sarega
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
16,027 Expert Mod 8TB
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
sarega
82
It does not submit the form when clicked
Jan 30 '09 #21
acoder
16,027 Expert Mod 8TB
Have you set the action of the form and is the div inside a form?
Jan 31 '09 #22
sarega
82
yes I have, its working for mozilla but not in IE
Feb 1 '09 #23
acoder
16,027 Expert Mod 8TB
Post the HTML code or at least some test code demonstrating the problem.
Feb 1 '09 #24
sarega
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
sarega
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
16,027 Expert Mod 8TB
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
sarega
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
16,027 Expert Mod 8TB
You still haven't posted the HTML code.
Feb 5 '09 #29
sarega
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
sarega
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
16,027 Expert Mod 8TB
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
sarega
82
I do not have a submit button before am creating it dynamically later
Feb 8 '09 #33
acoder
16,027 Expert Mod 8TB
Then what about the code you posted in #30?
Feb 9 '09 #34
sarega
82
I create that dynamically after the response is returned from the ajax
Feb 9 '09 #35
acoder
16,027 Expert Mod 8TB
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
sarega
82
Sorry that cannot be done because its confidential...
what do you want me to post?
Feb 9 '09 #37
acoder
16,027 Expert Mod 8TB
Either PM me the link or attach the files here (removing sensitive/confidential info).
Feb 9 '09 #38

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

Similar topics

4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
4
by: Sileesh | last post by:
Hi I have datagrid with 8 items. Out of which 2 items are checkboxes. Data is binded dynamically to the datagrid Based on some values from the database I have to check or uncheck the checkbox...
27
by: ted benedict | last post by:
hi everybody, i hope this is the right place to discuss this weird behaviour. i am getting dynamically generated text or xml from the server side using xmlhttprequest. if the server side data is...
6
by: bradb | last post by:
Hello, I have a textbox with an empty span element (place holder) next to it. When the user adds some text to the text box, I create a checkbox in the empty span element. When I create the...
5
by: stellstarin | last post by:
I have a html where fields are created and added dynamically on the client side. I use the AppendChild() call to create fields dynamically. On submit i try to get the value for all the...
4
by: sydney.luu | last post by:
Hello, I would greatly appreciate if someone can show me how to dynamically build a Repeater with unknown number of columns at design time. I have looked various threads in this newsgroup,...
4
by: sydney.luu | last post by:
Hello All, I am programmatically building a table on my web page with one row and two columns. The first column contains a web server checkbox dynamically created and the second column simply...
21
by: Leena P | last post by:
i want to basically take some information for the product and let the user enter the the material required to make this product 1.first page test.php which takes product code and displays...
2
by: ssmith147 | last post by:
Hi, I'm somewhat familiar with access and vb programming (I can read someone else's code, for the most part), but I'm still very green when it comes to creating solutions for my own needs. I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.