Jump to content

Replacing Characters??


BenC

Recommended Posts

I'm pretty new to JavaScript, so my question might be pretty obvious to you pros. I'm trying to find out if you can replace a specific character in a variable or string. For example, can the replace() and the charAt() tags be combined? I've come up with something like this just to test this out, but it's obviously not working:(in the head)var lstpages = "0000000";(in the body)document.write(lstpages.replace(charAt(3)), "1");document.write(lstpages.charAt(3)); The last line is used only to test if it workedAm I going about this wrong? My goal is to have X (7 in this example) number of pages, and on each page they will represent one of the characters in "lstpages" and change the character from 0 to 1 when visited, then ultimatly see if lstpages=1111111. Any advice appreciated!

Link to comment
Share on other sites

Ok, so I've gotten a little closer to my goal. Using this code:if(lstpages.charAt(3) == '0' ){ lstpages = lstpages.replace(lstpages.charAt(3), '1')}works IF I change the string to all letters, and instead of a '0' and '1' in that code change them to letters as well, but it doesn't return the right results with all numbers. I have a feeling that I'm just missing 1 small detail somewhere. so close :)

Link to comment
Share on other sites

ok in that case it should work...I didn't think of that.Your code that manages this variable will have to be on the page that contains the iframe...then on each page that will load in the iframe execute a function that will change the value of the variable on the page with the iframe.Am I making sense?

Link to comment
Share on other sites

You're absolutly making sense, and that's exactly what I'm trying to do. I just can't get it to replace a specific character within the variable. I think I'm having better luck trying it with letters instead of numbers. I'll keep doing this trial and error strategy I seem to have going, unless you can help me out with the code. I appreiciate your help!

Link to comment
Share on other sites

Try this out....function replaceChars(entry) { out = "a"; // replace this add = "z"; // with this temp = "" + entry; // temporary holder while (temp.indexOf(out)>-1) { pos= temp.indexOf(out); temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length)); } document.Form1.text.value = temp; }<input type=text name=text size=40 value="abcdabcd"><input type=button name=action value="Click" onClick="replaceChars(document.Form1.text.value);">

Link to comment
Share on other sites

Thanks for the reply pulpfiction, I'm not sure that's what I'm trying to do though.....Here's where I'm at. For now I have it on the same page until I get it to work, then I'll tackle the iframe part of my problem......IN THE HEAD<script language="javascript">var quizes = 'aaaaaaa'; //sets the variable</script>IN THE BODY<script language="javascript">if(quizes.charAt(3) == 'a' ){ //sees if it's an aquizes = quizes.replace(quizes.charAt(3), 'b') //replaces it with a be if it is}document.write(quizes); //just for testing to see if it worked</script>The problem I'm having with that is that no matter what I put in the charAt() on the replace line, it replaces the 1st (or 0) character.

Link to comment
Share on other sites

Well I figured it out, just took a different approach to it, how's this look? IN HEAD OF MAIN PAGEquiz = new Array ("a","a","a","a","a","a");IN BODY OF EACH PAGE, THE [2]'S WILL CHANGE FOR EACH ONEif (parent.quiz [2] == 'a' ){ parent.quiz[2] = "b";}Thanks guys!

Link to comment
Share on other sites

Well I figured it out, just took a different approach to it, how's this look? IN HEAD OF MAIN PAGEquiz = new Array ("a","a","a","a","a","a");IN BODY OF EACH PAGE, THE [2]'S WILL CHANGE FOR EACH ONEif (parent.quiz [2] == 'a' ){ parent.quiz[2] = "b";}Thanks guys!

That seems ot be the best approach anyway...arrays are much easier to manage.Good job.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...