0

I'm not sure why this code doesn't work. It may be because the checkbox is on a coldfusion div page. When the checkbox is clicked, I want to update the individuals record.

The ReInitialize function works when I click on that button. The code to zebra stripe the table works on initial form load, but not on subsequent loads when it posts to itself. The checkbox click function never gets hit. I can set a breakpoint in Firebug and it never stops.

<script type="text/javascript">
$(document).ready(function () {
    $('#ReInitialize').click(function () {
        var ReInitAnswer = confirm('Are you sure you want to start over from scratch?');
        if (ReInitAnswer) {
            $.ajax({
                type: "POST",
                url: "cfc/basic.cfc?method=TruncateTemp",
                error: function (xhr, textStatus, errorThrown) {
                    // show error
                    alert(errorThrown);
                } /*
                ,
                success: function (msg) {
                    alert("Data Saved: " + msg);
                } */
            });
            $('#ReInitialize').attr('checked', true);
        } else {
            alert('canceled');
        }
    });

    var table = $('#articles'),
        rows = table.find('tr'),
        cells, background, code;

    for (var i = 0; i < rows.length; i += 1) {
        cells = $(rows[i]).children('td');
        code = $(cells[0]).value();
        if (code % 2 == 0) {
            background = '#e29e6e';
        } else {
            background = '#f9cf80';
        }
        $(rows[i]).css('background-color', background);
    }

    $('.notdupe').bind('change', function () {  // <----------------------
        // If checked
        if ($(this).is(":checked")) {
            $.ajax({
                type: "POST",
                url: "cfc/basic.cfc?method=SetNotDupe",
                data: "indivNum=" + $(this).value() + "&SetValue=" + $(this).checked(),
                error: function (xhr, textStatus, errorThrown) {
                    // show error
                    alert(errorThrown);
                }
            });
        }
    });
});
</script>

<cfif qPages.not_dupe_flag>
    <input class="notdupe" type="checkbox" name="indivID" value="#userid#" checked />
<cfelse>
    <input class="notdupe" type="checkbox" name="indivID" value="#userid#" />
</cfif>

I changed it from a click event to a change event.

7
  • 3
    If you can provide the html once it hits the browser, that would be more useful. Commented Jan 19, 2012 at 18:12
  • 1
    what is <!--- and ---> supposed to do? it's not a valid js comment. use /* and */ Commented Jan 19, 2012 at 18:14
  • 2
    and additional to @JamesMontagne's comment: proper formatting (indention) helps readability. Commented Jan 19, 2012 at 18:15
  • @roberkules <!--- ---> are CFML (Cold Fusion Markup Language) comments. They are server-side so anything within them will not be output to the browser. Commented Jan 19, 2012 at 18:26
  • 1
    if you're too lazy - no offense :) - to do the indention: just drop your code @ jsbeautifier.org and hit the beautify button. Commented Jan 19, 2012 at 19:33

2 Answers 2

1

It looks like it should bind correctly. But I think there is an issue with scope with the AJAX call inside the $('.notdupe').bind() function.

Within the $.ajax.data code, I think the $(this) no longer refers to $('.notdupe') but to the AJAX event. Also, I'm not sure what .checked() is supposed to do.

$('.notdupe').bind('change', function (e){
    // If checked
    if ($(this).is(":checked")) {
        $.ajax({
            type: "POST",
            url: "cfc/basic.cfc?method=SetNotDupe",
            data: "indivNum=" + $(e.target).value() + "&SetValue=" + $(e.target).is(":checked")),
            error: function (xhr, textStatus, errorThrown){
                // show error
                alert(errorThrown);
            }
        });
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm trying to update the individuals record with the value of the checked attribute (true of false).
Ok... so is there a .checked() method you define somewhere else? If not, it should throw an error. I changed it into $(e.target).is(":checked")
0

Have you tried using .on() instead of .click()?

Replace:

$('#ReInitialize').click(function() {

With:

$('#ReInitialize').parent().on('click', '#ReInitialize', function() {

I am not clear what all of your code does, but if #ReInitialize gets replaced at some point, you will lose your click handler. This implementation of .on() will apply to #ReInitialize and any future instantiations.

P.S. if you are using an older version of jQuery, it would be .live('click', function() {

8 Comments

Not my downvote, but what would changing click to on accomplish?
I thought he was saying that the first click worked, but not the second. If his #ReInitialize element got replaced by some ajax response, then the on() would keep the handler attached to the DOM and not the non-existant original element, no?
Sorry, <!--- ---> is a Coldfusion comment.
@JeffB Nope. $(<selector>).on(<event>, <event-handler>) is the same as $(<selector>).bind(<event>, <event-handler>). To delegate the binding you need $(<root-element>).on(<event>, <selector>, <event-handler>);. If you don't set a <root-element> then you aren't delegating. As a note: .live() is depreciated so using it is not a good forward thinking policy, instead use .delegate with document as the <root-element>: $(document).delegate(<selector>, <event>, <event-handler>);
That's actually not true. on used the way you have it there would do the same as click. You are referring to event delegation. That would be $("ancestor").on("click", "#ReInitialize", function...) where ancestor is a selector of some ancestor element which will not be replaced.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.