Solved

jquery checkbox change

Posted on 2014-02-13
6
551 Views
Last Modified: 2014-02-24
function onxyg(){
                                                 
if(true){
        alert("is true");
        $(chngid).prop("checked", false).change();
       alert("done change");
        return;
 }
}

at checkbox I am doing onChange and calling onxyg java script function. But every time when I call "$(chngid).prop("checked", false).change();" it is triggering onChange again and looping ;

Please let me know if any one has better idea to work on this.
0
Comment
Question by:nzdjava
6 Comments
 
LVL 19

Expert Comment

by:Rikin Shah
Comment Utility
Hi,

Have you tried-
$(chngid).toggle(this.checked);
0
 
LVL 19

Expert Comment

by:Rikin Shah
Comment Utility
Hi,

You can also do-
$(chngid).attr("checked",true/false);
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
What is the goal of your code?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 42

Accepted Solution

by:
Chris Stanyon earned 500 total points
Comment Utility
There's a lot wrong with your code

Your if statement will always fire:

if (True)

You're supposed to check whether 'something' is true

The reason your code fires the change again is because you're asking it to with the change() method:

$(chngid).prop("checked", false).change();

You refer to a variable called 'chngid' but never declare it anywhere or pass it into your function.

On top of that, it looks like your code is trying to prevent a tickbox from being ticked. Is that right!
0
 

Author Comment

by:nzdjava
Comment Utility
Thanks for all your answers.

Sorry to short cut my code here in this question.  In real code I am checking if check box is cheked is true and I am checking that as  True. And if it is true I want to make it false and also change the label of the Checkbox(IphoneStyle).

on the checkbox code in html I am calling onchange and calling the javascript method.

Variable chngid is the checkbox html element. I declared it on top of my code.
0
 
LVL 42

Expert Comment

by:Chris Stanyon
Comment Utility
Still doesn't really make a lot of sense. When someone changes the checkbox, you want to change it to false, thereby preventing it ever being true. Is that right?

If you want to make the checkbox false, then why bother checking whether it's true - just set it to false!

How to you tie up the checkbox with the label, so you can change the text

You shouldn't be calling code directly in the HTML - you should bind the event in jQuery

This will set the checkbox to false if someone tries to change it:

$('input[type="checkbox"]').change(function() {
    $(this).attr('checked', false);
});

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now