JQuery getElementsByName

I was recently informed of a bug in my turing test for comments on my site.  It turned out that there was a problem that the bit addition scheme when working with IE. Since about 4% of my visitors are IE users it never really came up; until it did. 🙂

Anyways, the way I was doing the test before was with  some basic bit shifting done in JavaScript like this

function mdfyBits(box)
{    
    el = document.getElementsByName("bits[]");
    
    val = 0;
    for (i in el) {
        if (el[i].checked) {
            alert(el[i]);
            val += 1 << el[i].value;
        }
    }
    
    $("#turing").val(val);
    
    alert($("#turing").val());
}

Even running IE in compatibility mode didn't fix it.  Turns out that I guess IE doesn't like that.  So I looked for a JQuery replacement and this is what I changed it to.

function mdfyBits(box)
{
    el = $("input[name=bits[]]");
    
    val = 0;
    for (i in el) {
        if (el[i].checked) {
            val += 1 << el[i].value;
        }
    }
    
    $("#turing").val(val);
}

Works now.  Dang browser compatibility.

Related posts

One thought on “JQuery getElementsByName

  1. Hey, thanks for this code, actually I was looking for it.

Leave a Comment