Alist3r
Alist3r

Reputation: 556

Strange Behaviour with change properties of checkboxes

I've this code:

<table border="1" style="margin:0 auto;">
    <tr>
        <td class="pointer" style="padding: 2px;"><input style="margin-left: 2px;" type="checkbox" name="nuovoCheck" id="newCheckId" value="N" /></td>
        <td id="textCheckNuovo" class="pointer" onClick="return selectNew();"><b>New</b></td>
    </tr>
</table>

with the associated script:

function selectNew() {
    if ($('#newCheckId').prop('checked') == false) {
        $('#newCheckId').attr('checked', true)
    }
    else {
        $('#newCheckId').attr('checked', false)
    }
}

I don't know why this work just for 1 times:

I don't understand.

I want this: if the text near the checkbox its clicked then the checkbox have to be checked

Upvotes: 0

Views: 69

Answers (3)

Rob Baillie
Rob Baillie

Reputation: 3470

If you can handle going with HTML5 compatibility, the "label" tag handles this, ties the text semantically (I.E. it is understood to be linked actually as a label for the checkbox), and works without javascript (for browsers that are HTML5 compliant)

E.g.

<table border="1" style="margin:0 auto;">
    <tr>
        <td class="pointer" style="padding: 2px;"><input style="margin-left: 2px;" type="checkbox" name="nuovoCheck" id="newCheckId" value="N" /></td>
        <td id="textCheckNuovo" class="pointer"><label for="newCheckId">New</label></td>
    </tr>
</table>

See this JS fiddle (without javascript) for an example in action: http://jsfiddle.net/RKuhT/

Upvotes: 1

Jasper Van Kerschaver
Jasper Van Kerschaver

Reputation: 81

try this:

    if ($('#newCheckId').prop('checked') == false) {
        $('#newCheckId').prop('checked', true)
        $('#newCheckId').attr('checked', true)
    }
    else {
        $('#newCheckId').removeAttr('checked')
    }

Upvotes: 0

Sudhanshu Yadav
Sudhanshu Yadav

Reputation: 2333

Try

function selectNew() {
            if ($('#newCheckId').prop('checked') == false) {
                $('#newCheckId').prop('checked', true)
            }
            else {
                $('#newCheckId').prop('checked', false)
            }
        }

Upvotes: 1

Related Questions