Dragen Posted November 21, 2007 Share Posted November 21, 2007 grr.. I can't figure out my problem. I've got a function set to an onclick, which runs am exec search. If the search is successful, it then replaces the found part with.. well. blankness. function changepic(img, name, val){ var setsearch; var img_src = document[img].src; name = name.toUpperCase(); setsearch = /\W/g; result = setsearch.exec(val); if(result == null){ alert(result+' '+val); }else{ val = val.replace(result, ""); alert(result+' '+val); } } setsearch is an ereg search for any non-alpha character. If it's null (not in the searched variable) then I alert the result and val. If it is successful, then I remove the characters from val. And alert the results. Now the problem. The first time I run the script it works fine. The second it comes out as null, third it works, fourth it doesn't etc.. I'm sending the same value each time, and it shouldn't return null for it. This is how I'm calling it: <input type="text" name="bg" value="#06d6a6" id="bg" onfocus="javascript:changepic('prev', 'BG', this.value);" /> so it should replace the hash (#) each time, but for some reason it does it every other time.. Any ideas why it's doing this? Thanks Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 23, 2007 Author Share Posted November 23, 2007 anyone? I'm really stuck on this.. I can't see any reason why it only works every other time. Quote Link to comment Share on other sites More sharing options...
Dragen Posted November 23, 2007 Author Share Posted November 23, 2007 again.. solved The problem was that I was using setsearch as a simple variable: setsearch = /\W/g; when I should have been setting it as a RegExp: setsearch = new RegExp(/\W/g); Which was giving me the strange result of working exactly every other time... heh. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.