Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jquery checkbox change

Posted on 2014-02-13
6
Medium Priority
?
612 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 19

Expert Comment

by:Rikin Shah
ID: 39858173
Hi,

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

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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

704 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