Solved

jquery checkbox change

Posted on 2014-02-13
6
573 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
Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

 
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

Report: Liquid Web beats Amazon, Rackspace & More

A study by performance analyst firm Cloud Spectator finds that Liquid Web beats rivals Amazon, Rackspace and DigitalOcean when it comes to website and cloud application performance.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Javascript: Mouseup doesn't follow mousedown 13 61
Javascript: Range object 16 34
CSV export single column not inserting comma's in file 11 67
Jquery Calculation 3 42
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…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
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…

751 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