0

I'm new to JS, can't understand how to make this code work. I'm trying to overwrite the whole html source text.

var oldSource = document.documentElement.innerHTML;
alert(oldSource); //this works, it gets the whole html

document.write(Change(oldSource)); //doesn't work, writes undefined

   function Change(source){
     for (i = 0; i <= source.length; i++){
     source[i] = "S"; // doesn't change the source[i]
   }
}
5
  • 1
    You might want to take a look at JQuery. It's a javascript library that makes manipulating the DOM (i.e. the html on a web page) easier. Adding the line <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script> to the <head> of your html file imports the library. Once it's imported you can call .html() on a DOM element and pass in the new HTML you'd like it to contain. Note JQuery is used on over 55% of all web pages, according to wikipedia. Commented Jul 9, 2013 at 0:13
  • Are you trying to change the entire document to a bunch of S characters as you're iterating over a string, and if so, why ? Commented Jul 9, 2013 at 0:17
  • A function named Change??? Give your functions decent names so we can try to understand what you're trying to do. Commented Jul 9, 2013 at 0:18
  • 1
    @Accipheran: Not being easy enough isn't the issue here. Not every page needs jQuery. Commented Jul 9, 2013 at 0:22
  • 1
    @adeno its probably just an exercise to see what happens. The OPs problem isn't what the function is doing but why the innerHTML wasn't updating. Commented Jul 9, 2013 at 0:24

6 Answers 6

2

You are changing the value of the variable oldSource, not the value of the documentElement.innerHTML.

.innerHTML just returns a string that contains the serialised content of that element. It doesn't return a reference to the content in the DOM.

Furthermore, document.write(Change(oldSource)) is saying write the return value of Change(oldSource) to the document... but your Change() function doesn't return anything, hence it is undefined.

Lastly, strings are immutable, meaning you can't change their contents after they have been created. Instead, you need to build up a new string in your function, like so:

function Change(source){
   new_source = ""
   for (i=0; i < source.length; i++){
     new_source = new_source + "S"; //doesn't change the source[i]
   }
   return new_source
}

You can check all of this out in this jfiddle.

Sign up to request clarification or add additional context in comments.

11 Comments

You can change the innerHTML of documentElement.
@AndyG Hmmm, I had trouble doing it in JSFiddle and didn't verify. I'll edit that out.
No worries. It needs to be < source.length (and JS statements are semi-colon terminated - sorry, I know it works anyway without them.)
@AndyG Such a pedant :P Fixed the condition, but I didn't add the last semicolon as its the last instruction in the block and won't be unambiguous :)
You mean it "will be unambiguous" ;) Yes, you could describe me as a pedant :)
|
0

You need to return( source ); after your for loop

1 Comment

JavaScript strings are immutable, so this will just return the original text.
0

JavaScript strings are immutable. You can't use

source[i]="S";

to modify a string. You need to build a new string and return it.

It should be < source.length as well.

Comments

0

In JavaScript, this line: var oldSource = document.documentElement.innerHTML; copies the innerHTML into the oldSource variable. It does not save a reference to the innerHTML.

You are modifying the value stored in oldSource, but never applying it to the document.

In your Change function, you do not return any value - that is why you are getting an undefined. Add a 'return source;in yourChange` function to return a value.

The other problem is that you can't change the string in JavaScript. You should make a new variable for this. You can't edit source[i] - you can only retrieve the value that is there.

1 Comment

You can use [] to access characters of a string, but strings are immutable so we can't use this to change them.
0

You need to return the new source string like below;

var oldSource = document.documentElement.innerHTML;
alert(oldSource); //this works, it gets the whole html

document.write(Change(oldSource)); //doesn't work, writes undefined

function Change(source){
     for (i=0; i<=source.length; i++){
         source[i]="S"
     }
     return source;
}

Comments

0
  1. your function must return something, not just change
  2. you cannot change strings this way, use .replace() method instead
  3. if you specify what you really need to do, our help could be more effective

EDIT As far as I didn't find correct working code here, I want to suggest my one

function Change(source){
   var str = source.split("");
   for (var i=0; i<source.length; i++){
     str[i] = "S";
   }
   return str.join("");
}

Maybe it's not the fastest way (though, why not) but it lets you to operate with indexes as you tried in your question.

working fiddle for Lego Stormtroopr

EDIT2 and this example shows how to do it in one line inside the loop (without creating extra variables)

   for (var i=0; i<source.length; i++){
     source = source.replace(new RegExp(source[i]), "S");
   }

2 Comments

-1 for linking to W3Schools. Functions don't always have to change things, its just in this case it does. It could just as easily pass in an id, call getElementByID and change via that, no return neccessary. Also, it appears that the code is exploratory, not homework or production. I think the OP just wants to learn why this doesn't work.
@LegoStormtroopr then write your own best answer, ah you already did it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.