Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

jquery checkbox change

Posted on 2014-02-13
6
Medium Priority
?
634 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 20

Expert Comment

by:Rikin Shah
ID: 39858173
Hi,

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

Expert Comment

by:Rikin Shah
ID: 39858176
Hi,

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

Expert Comment

by:leakim971
ID: 39858668
What is the goal of your code?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 44

Accepted Solution

by:
Chris Stanyon earned 2000 total points
ID: 39858723
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
ID: 39859096
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 44

Expert Comment

by:Chris Stanyon
ID: 39859132
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
Suggested Courses

916 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