Solved

jquery checkbox change

Posted on 2014-02-13
6
581 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
How To Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 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 43

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

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…
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…
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

623 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