SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Aug 2011
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to build in array subfunction in this setAttribute function?

    Hi, I have written a function that automatically inserts an attribute (i.e. a per case adapted onclick function) in input elements. It also makes a few exceptions. It looks like this, somewhat simplified for clarity purposes:
    HTML Code:
    function insertAttribute() {
        var allInputs = document.getElementsByTagName('input');
        var allInputsCount = allInputs.length;
        var thatInput = null;
        for (i = 0; i < allInputsCount; i++) {
            thatInput = allInputs[i];
            var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
            if ((thatInput.name != "A") && (thatInput.name != "B") && (thatInput.name != "C"))
                thatInput.setAttribute("onclick",highlightFunction);
        }
    }
    The problem is, there are some 20 input elements where the attribute should not be inserted in. I could expand the if line, but I would rather do it with an array. But how do I do that? I googled how to use array in javascript function which gave me two top results (this and this) that suggested I should do it like this:
    HTML Code:
    function insertAttribute() {
        var allInputs = document.getElementsByTagName('input');
        var allInputsCount = allInputs.length;
        var thatInput = null;
        for (i = 0; i < allInputsCount; i++) {
            thatInput = allInputs[i];
            var highlightFunction = "highlightItem('"+thatInput.name+"-row','"+thatInput.name+"-button')";
            var exceptedArray = ["A","B","C"];
            if (thatInput.name != exceptedArray)
                thatInput.setAttribute("onclick",highlightFunction);
        }
    }
    But that doesn't work -- the attribute is still inserted. How should it be done?

  2. #2
    SitePoint Zealot
    Join Date
    Aug 2011
    Posts
    114
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got an answer elsewhere that seems to work fine: the if line should read:
    Code JavaScript:
    if (exceptedArray.indexOf(thatInput.name) == -1)
    .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •