2

When using JavaScript to set the innerHTML of a div I have just one div that I cannot figure out why it is not being set...

I have the following code in my <body> on my page

<div style="width:50%; margin:0 auto;">
    <div id="name"></div>
    <div id="image"></div>
    <div id="description"></div>
</div>
<script>
    var url = window.location.href;
    var captured = /name=([^&]+)/.exec(url)[1];
    var result = decodeURI(captured);
    if (result != 'none') {
        var name = document.getElementById('name');
        name.innerHTML = "<p>" + result + "</p>";
        var description = document.getElementById('description');
        description.innerHTML = '<object data="descriptions/' + result + '.txt" style="font-family:Gotham, Helvetica, Arial, sans-serif"></object>'
        var image = document.getElementById('image');
        image.innerHTML = '<img style="float:left;" src="images/' + result + '.jpg">'
    }
    else {
        document.write('You have no items in your cart');
        var elem = document.getElementById('badge');
        elem.parentNode.removeChild(elem);
    }
</script>

For some reason the name <div> does not get modified when the description and image divs do. Is there something I am missing here?

The JavaScript console in Safari and Chrome shows no errors and both have the same result. The name variable is also not null.

Also note this is just a school project, this doesn't have to be practical by any means.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Mistah_Sheep
  • 785
  • 6
  • 25

1 Answers1

5

The name variable conflicts with window.name. You simply need to change var name = to var somethingElse =.

Tony Hinkle
  • 4,706
  • 7
  • 23
  • 35
  • Would have never found this otherwise...Thank You! – Mistah_Sheep Apr 19 '16 at 20:48
  • How did this get 6 upvotes? Of course you can use `name` as an ID. You can also declare `name` variables, since `name` is definitely not a reserved word. The only problem is the conflict with `window.name` in the global scope. – Oriol Apr 19 '16 at 20:54