views:

443

answers:

3

Hey Guys -

I'm at my wit's end with this.

Can anyone see anything wrong with this line? The function won't fire by clicking on the checkbox for some reason, but the calling function works fine (if I copy the exact "onclick" attribute over to the label for the checkbox, it works fine).

<input type="checkbox" name="match_35_0" id="match_35_0d" value="d0" onclick="checkSwap(document.page_form.match_35_0d, document.page_form.match_35_0)"></input> 

If anyone can see why on earth this wouldn't be working, I would really appreciate it.

Thanks!

EDIT: Since a couple people asked, here's the checkSwap function (all it does is throw an alert so I can see that my onclicks are working before I add any code):

function checkSwap(radioid, groupid) {
alert("radio: " + radioid + " group: " + groupid);}

And here's the whole sample of the table cell that the checkbox in question is in (apologies for the formatting, the code sample doesn't seem to want to accept my newlines):

<td><label onclick="checkSwap(document.page_form.match_34_0d,document.page_form.match_34_0)" for="match_34_0">N</label><input type="checkbox" name="match_34_0" id="match_34_0d" value="d1"  onclick="checkSwap(document.page_form.match_34_0d, document.page_form.match_34_0)"></input></td>

EDIT: Alright, canceling out a separate function that was limiting the checkboxgroup to 1 checked box was the issue.

The code that does the limiting was setting an onclick attribute for each checkbox, and that is apparently overriding the tag-set attribute. I'll have to figure out how to hack around it.

A: 

It would be easier to pass in this to the function, then the parameter would be a reference to the element that called the function. So you can do:

function checkSwap(self){
    alert(self.id);
}

Edit: Also, document.page_form.match_35_0.id will get the id, not the way you have it.

Jeremy
A: 

this function does fire - checked in firebug

<input type="checkbox" name="match_35_0" id="match_35_0d" value="d0" onclick="alert('55')"></input>

you have to check 'checkSwap'

fazo
+1  A: 

This syntax

document.page_form.match_35_0d

actually searches in the form with name of page_form for an input element with name of match_35_0d. But you have actually set it as an id of the checkbox where the onclick is definied.

You could solve your problem with the following call in the onclick:

checkSwap(this, document.page_form.match_35_0)

By the way, a checkbox is not the same as a radiobutton and you're actually not passing the ID's to the function, but instead the whole elements. Rewrite your function as

function checkSwap(checkbox, group) {
    var checkboxId = checkbox.id;
    for (var i = 0; i < group.length; i++) {
        var element = group[i];
        var elementId = element.id;
        // ...
    }
    // ...
}

To obtain an element by ID, just use Document#getElementById().

var element = document.getElementById('someId');
BalusC
Ah, I wasn't aware of that (haven't done much JS, as I'm sure you can tell).You have me passing in 'this' as an argument - it makes sense called from the checkbox's onclick property. However, what would be the correct syntax for accessing that specific checkbox by id? I have a label that I'd also like users to be able to click on to somehow run my checkswap function - from what I can tell, the easiest way for me to do that would just to be to call checkbox.onclick, but I'm not sure how to point at the specific checkbox object by id.
javanix
Use `document.getElementById('id')`.
BalusC
Excellent, that seems to work fine. Thanks for the help.
javanix