Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
113
rated 0 times [  117] [ 4]  / answers: 1 / hits: 35175  / 11 Years ago, tue, october 29, 2013, 12:00:00

I know that several questions about this topic have been asked, but I was unable to find an answer for my case.



I have a checked checkbox as



<input type=checkbox name=text checked=checked />


I need to send ajax request by checking/unchecking, but I do not know what can be the reliable (with browser compatibility) for an if statement such as



if (this.value == 'on')
{
this.value = 'off';
ajax call;
}
else
{
this.value ='on';
ajax call;
}


Note that the value is not important here, and we need to catch checked/unchecked, but how control the checked element by JavaScript when the html original element has checked=checked?



If using checked instead of value as



if (this.checked == true)
{
this.checked = false;
ajax call;
}
else
{
this.checked =true;
ajax call;
}


The tick of checkbox will not be changed in the browser (always ticked).


More From » checkbox

 Answers
20

The following should work:



function myFunction(elem)
{
if (elem.checked)
{
alert(Im Checked);
}
else
{
alert(Im not checked);
}
}


Markup:



<input type=checkbox name=text checked=checked onchange=myFunction(this); />


http://jsfiddle.net/QMhn5/



Update:
To change the check from other element:



document.getElementById(chk).checked = true;


or



document.getElementById(chk).checked = false;


Example:
http://jsfiddle.net/QMhn5/1/


[#74637] Monday, October 28, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
breonnamayah

Total Points: 574
Total Questions: 115
Total Answers: 96

Location: England
Member since Sun, May 21, 2023
1 Year ago
breonnamayah questions
;