Ray S.
Ray S.

Reputation: 1200

jquery - retrieving changes to a form

I am trying to retrieve the html of the "today_div" as it looks like in the browser.

Problem is that even if the user clicks on the checkbox, the html of the div does not show "checked" in the checkbox html when retrieving as below.

How can one retrieve the html with the changes?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<script type="text/javascript" src="./js/jquery-1.10.2.min.js"></script>
</head>
<body>

<div id="today_div">
<input type="checkbox" name="as" class="achk">
</div>

<script language="JavaScript" type="text/javascript" defer=true>

$(document.body).on( "change",'.achk', function() {
    var s = $('#today_div').html();
    alert(s);
});

</script>

</body>
</html>

UPDATE: anyone know why the above does not work? if the user checked the checkbox that is not enough? he must also set it in javascript. very strange.

Upvotes: 1

Views: 73

Answers (2)

Rituraj ratan
Rituraj ratan

Reputation: 10378

$(document).on( "change",'.achk', function() {
    if($(this).is(":checked")){
       $(this).attr("checked","checked");
    }else{
       $(this).removeAttr("checked");
    }
    var s = $('#today_div').html();

    alert(s);
});

see demo

reference attr

Upvotes: 1

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

do like:

$(document.body).on( "change",'.achk', function() {
    var $this = $(this),
        is_checked = $(this).attr("checked") ? "checked" : "";
    $this[0].setAttribute("checked", is_checked);
    alert($('#today_div').html());
});

Demo :: jsFiddle

Upvotes: 2

Related Questions