0

So I'm trying to get my website to find certain text and replace it with an <img> tag using this code...

function Emote() {
    var stringg = document.getElementsByClassName("Post").innerHTML;
    var resolutionn = stringg.replace("xD", "<img src='Icons/xD.gif' width='100px' height='100px' />")
    document.getElementsByClassName("demo").innerHTML = resolutionn;
}
Emote();

When I debug with Firebug it outputs this...

enter image description here

0

2 Answers 2

3

The function getElementsByClassName returns an array of elements, rather than a single element. Arrays do not have the property innerHTML, thus such will be undefined.

If you want to apply this function to the first element matching the class, use:

document.getElementsByClassName("Post")[0].innerHTML

instead.

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

1 Comment

this is not the complete solution guys
1

The return-value of document.getElementsByClassName("Post").innerHTML is undefined.

That because document.getElementsByClassName is retouring an array, not a single Element.

Use document.getElementsByClassName("Post")[0].innerHTML to access the first:

<script>
    function Emote() {
        var stringg = document.getElementsByClassName("Post")[0].innerHTML;
        var resolutionn = stringg.replace("xD", "<img src='Icons/xD.gif' width='100px' height='100px' />")
        document.getElementsByClassName("demo").innerHTML = resolutionn;
    }

    window.addEventListener("load", Emote);
    // or window.onload = Emote();
</script>

9 Comments

I thought it would work but it now gives me this output... imgur.com/H6TWCtE
so this tells you that, there was no Element with this className found in your document. Consider it's case-sensitive, maybe you use "post" not "Post" ?
Nope, my html is p class="Post", my CSS works with .Post{} so I don't understand why this isn't working o.o.
Umm, well it's a whole web-page so, view-source:insanewolfhd.byethost17.com
Oh. I guess you are executing the Script before the HTML? So it will not be there in the moment the script executes. Use window.onload (updated my ansewer)
|

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.