Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-03-2010, 10:08 PM   PM User | #1
bwalls
New to the CF scene

 
Join Date: Oct 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
bwalls is an unknown quantity at this point
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>
bwalls is offline   Reply With Quote
Old 10-03-2010, 11:39 PM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 960
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Old 10-03-2010, 11:53 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,206
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 10-03-2010, 11:54 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,206
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 10-04-2010, 12:43 AM   PM User | #5
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 960
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
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.
Logic Ali is offline   Reply With Quote
Reply

Bookmarks

Tags
regex, test

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.