Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expression headache

    I have a function that changes the label on a set of labels with similar "for" fields. Somehow, the matching is only matching every other label. I suspect I must be missing something obvious.

    The goal is to change the text of a set of radio buttons to either "top" and "bottom" or "left" and "right" depending on orientation.

    Code:
    function ChangeLabels(orientation) {
    	var lefttop = 	"Left";
    	var bottomright = 	"Right";
        var lbls = document.getElementsByTagName("label");
        var lefttopregex = new RegExp ("Field6_[0-9]+", "gi");
        var bottomrightregex = new RegExp ("Field20_","gi");
        
        
        if (orientation == "Vertical") {
        	lefttop = "Top";
        	bottomright = "Bottom";
        }
    
      var forvalue = "";
      for (var i=0,k=lbls.length; i<k; i++) {
         	forvalue = lbls[i].htmlFor;
        
       	if (lefttopregex.test(forvalue)) {
          		lbls[i].innerHTML = lefttop;
        }
       	if (bottomrightregex.test(forvalue)) {
          		lbls[i].innerHTML = bottomright;
        }
         
      }	
    	
    }
    And here is the HTML of the labels.

    Code:
    <li id="fo1li6" class="     ">
    <label class="desc" id="title6" for="Field6_0">
    Left/Top
    <span id="req_6" class="req">*</span>
    </label>
    <div>
    <input id="radioDefault_6" name="Field6" type="hidden" value="" />
    <span>
    <input id="Field6_0" name="Field6" type="radio" class="field radio" value="Dogs" tabindex="20" onchange="handleInput(this);"  checked="checked" />
    <label class="choice" for="Field6_0" >
    Dogs</label>
    </span>
    <span>
    <input id="Field6_1" name="Field6" type="radio" class="field radio" value="Pineapple" tabindex="21" onchange="handleInput(this);" />
    <label class="choice" for="Field6_1" >
    Pineapple</label>
    </span>
    <span>
    <input id="Field6_2" name="Field6" type="radio" class="field radio" value="Leaves" tabindex="22" onchange="handleInput(this);" />
    <label class="choice" for="Field6_2" >
    Leaves</label>
    </span>
    <span>
    <input id="Field6_3" name="Field6" type="radio" class="field radio" value="Cat" tabindex="23" onchange="handleInput(this);" />
    <label class="choice" for="Field6_3" >
    Cat</label>
    </span>
    <span>
    <input id="Field6_4" name="Field6" type="radio" class="field radio" value="Star" tabindex="24" onchange="handleInput(this);" />
    <label class="choice" for="Field6_4" >
    Star</label>
    </span>
    <span>
    <input id="Field6_5" name="Field6" type="radio" class="field radio" value="Bamboo &amp; Sun" tabindex="25" onchange="handleInput(this);" />
    <label class="choice" for="Field6_5" >
    Bamboo & Sun</label>
    </span>
    <span>
    <input id="Field6_6" name="Field6" type="radio" class="field radio" value="Bamboo" tabindex="26" onchange="handleInput(this);" />
    <label class="choice" for="Field6_6" >
    Bamboo</label>
    </span>
    <span>
    <input id="Field6_7" name="Field6" type="radio" class="field radio" value="Sun &amp; Moon" tabindex="27" onchange="handleInput(this);" />
    <label class="choice" for="Field6_7" >
    Sun & Moon</label>
    </span>
    <span>
    <input id="Field6_8" name="Field6" type="radio" class="field radio" value="Fleur de lis" tabindex="28" onchange="handleInput(this);" />
    <label class="choice" for="Field6_8" >
    Fleur de lis</label>
    </span>
    <span>
    <input id="Field6_9" name="Field6" type="radio" class="field radio" value="Dragonflies" tabindex="29" onchange="handleInput(this);" />
    <label class="choice" for="Field6_9" >
    Dragonflies</label>
    </span>
    <span>
    <input id="Field6_10" name="Field6" type="radio" class="field radio" value="No Design" tabindex="30" onchange="handleInput(this);" />
    <label class="choice" for="Field6_10" >
    No Design</label>
    </span>
    </div>
    </li>

  • #2
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by bwalls View Post
    I have a function that changes the label on a set of labels with similar "for" fields. Somehow, the matching is only matching every other label.
    I don't think you need the g flag here and its use increments the lastIndex property of the regex.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Remove the "g" from "gi". Then it works.

    No, please don't ask me why.

    Clearly, there never was a reason for the "g". But as to why it causes the failure... My head hurts.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,121
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Quote Originally Posted by Logic Ali View Post
    I don't think you need the g flag here and its use increments the lastIndex property of the regex.
    Teach me to not hit refresh.

    Okay, I see it bumps lastIndex. But why does that matter, if you are doing a fresh call to test()???
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder Logic Ali's Avatar
    Join Date
    Sep 2010
    Location
    London
    Posts
    1,028
    Thanks
    0
    Thanked 207 Times in 202 Posts
    Quote Originally Posted by Old Pedant View Post

    Okay, I see it bumps lastIndex. But why does that matter, if you are doing a fresh call to test()???
    Because it's still pointing to the end of the last match, so on the next call it doesn't search from the start of the string.


  •  

    Tags for this Thread

    Posting Permissions

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