Link to home
Start Free TrialLog in
Avatar of Rozamunda
Rozamunda

asked on

how to do it in javascript ?

I have an ajax request returning :

  success : function(resp) {                   
                    document.getElementById('MyIdSpan').innerHTML = '';      
                          document.getElementById('MyId').innerHTML = resp;

now inside resp I have checkoboxes, i want to assing onclick event to all of them
In Jquery it would be like this:

        $('MyId).find('INPUT:checkbox').click( function() {                                    
                          $(this).parent("LABEL").toggleClass('checked', $(this).attr('checked'));
                          updatetext('MyID);            
                                });


The response I am getting is something like this:
<label class= "ui-state ">  <input name = "Ps[]" value = "1"  
		   			type = "checkbox"> 1           </label> <label class= "ui-state ">  <input name = "Ps[]" value = "4"  
		   			type = "checkbox"> 4          </label> <label class= "ui-state ">  <input name = "Ps[]" value = "2"  
		   			type = "checkbox"> 2 e    </label> <label class= "ui ">  <input name = "Ps[]" value = "3"  
		   			type = "checkbox"> 3 </labell> 

Open in new window

Avatar of Kim Walker
Kim Walker
Flag of United States of America image

I'm not sure what the question is. Are you wanting to replicate the jQuery script in basic javascript?

var myid, myidinputs;
function initializepage() {
	myid = document.getElementById("myID");
	myidinputs = myid.getElementsByTagName("input");
	for (var i=0; i<myidinputs.length; i++) {
		if (myidinputs[i].type == "checkbox") {
			if (window.attachEvent) {
				myidinputs[i].attachEvent('onclick',toggleclicked);		// IE
			} else {
				myidinputs[i].addEventListener('click',toggleclicked,false);
			}
		}
	}
}
if (window.attachEvent) {
	window.attachEvent('onload',initializepage);		// IE
} else {
	window.addEventListener('load',initializepage,false);
}
function toggleclicked() {
	if (this.checked) {
		this.parentNode.className += " checked";
	} else {
		this.parentNode.className = this.parentNode.className.replace(" checked","");
	}
}

Open in new window

Avatar of Rozamunda
Rozamunda

ASKER

yes, I want to replace jquery with javascript
so how to select  and attach an event to all of  the checkboxes which are just attached to DOM ?
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, thanks got it, and from any div it would be like above

myid = document.getElementById("myID");
      myidinputs = myid.getElementsByTagName("input")