0

In the code below the Save button click is not firing.

Any ideas why?

[code]

<script type="text/javascript">
$(function () {
    $(".Join").live("click", function () {
        var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
        html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

        $(this).closest("tr").after(html);
        return false;
    });

    $(".Save").live("click", function () {

        var json = { Password: $(this).closest("#pwd").val() };

        $.ajax({
            url: "/Group/ConfirmPassword",
            type: "POST",
            dataType: 'json',
            data: JSON.stringify(json),
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                if (result.Success == true)
                    window.location.href = result.Url;
                else
                    alert(result.ErrorMessage);
                return false;
            }
        });
    });
});

[/code]

1
  • Please provide an example with the button on jsfiddle.net Commented Mar 20, 2011 at 1:55

1 Answer 1

4

Because at the time you create the event handler for $(".Save")'s click, it does not exist. Use:

$(function () {
    $(".Join").live("click", function () {
        var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
        html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

        $(this).closest("tr").after(html);

        $(".Save").live("click", function(){
            var json = { Password: $(this).closest("#pwd").val() };

            $.ajax({
                url: "/Group/ConfirmPassword",
                type: "POST",
                dataType: 'json',
                data: JSON.stringify(json),
                contentType: "application/json; charset=utf-8",
                success: function (result) {
                    if (result.Success == true)
                        window.location.href = result.Url;
                    else
                        alert(result.ErrorMessage);
                    return false;
                }
            });
        });

        return false;
    });
});

EDIT: Based on findings in jsFiddle example...change the code you have already for loading in the JoinList to:

$("#grouplist").load(
    AppRoot + "Group/JoinList/",
    { page: page_num },
    function(){
        $(".Join").live("click", function () {
            var html = "<tr><td colspan='3'>Enter Password: &nbsp;&nbsp;<input type='password' id='pwd' />";
            html += "&nbsp;&nbsp;<input type='button' class='Save' value='Save' /></td></tr>";

            $(this).closest("tr").after(html);

            $(".Save").live("click", function(){
                var json = { Password: $(this).closest("#pwd").val() };

                $.ajax({
                    url: "/Group/ConfirmPassword",
                    type: "POST",
                    dataType: 'json',
                    data: JSON.stringify(json),
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        if (result.Success == true)
                            window.location.href = result.Url;
                        else
                            alert(result.ErrorMessage);
                        return false;
                    }
                });
            });

            return false;
        });
    }
);
Sign up to request clarification or add additional context in comments.

5 Comments

The site is only local to my PC.
ok give some time to register and add it, i have never used it.
After my page loads I call a js file to create a table etc. So how do add that to jsFiddle? So its dynamic, the html source does not have the markup of the table. I dont know to setup jsFiddles in this scenario.
OK I figured it out. $(".Join") does not exist either because you are dynamically loading in the HTML that creates it. See my edit.
If I run this script $(".Save").val() I get "Save" so it is in the DOM but no click occurring.

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.