www.webdeveloper.com
Results 1 to 3 of 3

Thread: No Firebub Error but Script not Working

  1. #1
    Join Date
    Feb 2013
    Posts
    12

    No Firebub Error but Script not Working

    Hi all,
    I'm very new to javascript and I'm practicing the creation of dynamic contents by using getElementByID and innerHTML and ran into a problem. I used Firebug to check for errors and didn't see any but my script is not working.
    When the page loads, my script is supposed to create new contents for a div with the id of Client_Info if it does not contain any inside it.

    The following are my the contents of my external javascript file called CreateTag.js and my HTML Markup:
    function Delete_Div()
    {

    var myElement= document.getElementbyID(Client_Info);

    //Onload Check if the contents of Client_Info is equal to the value in the parenthesis
    if(myElement.innerHTML!="")
    {
    //Rewrite the contents with the following values
    myElement.innerHTML=="<div id='Display' style='position:relative;width:100%;'>
    <img alt='Faces' src='images/Clients/YourFullAd.jpg' id='Gallery' style='position:relative;margin-left:45%;top:10px;' >
    <div id='ScopeCap' style='position: relative; margin-left:45%; top:65px;'> </div>
    </div>"
    }

    }

    <body onLoad = "Delete_Div()" onUnload = "Delete_Div()">
    <div ="Client_Info">
    <div id='Display'><img alt="Pets src='images/Clients/myDod.jpg' id='Gallery' ></div>
    </div>
    </body>

    Please take a look and see where I went wrong, thanks in advance.

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    There are syntax errors in the markup and the script.

    Either you have FireBug disabled or you're not using it properly.

    Try the native error console: Ctrl + Alt + J

    Here's the error that won't be indicated:

    Code:
    myElement.innerHTML=="<div id='Display' style=...............

  3. #3
    Join Date
    Feb 2013
    Posts
    46
    That's a very tricky error. JavaScript doesn't support multiline strings. You can use \ to escape the newLine character, but that makes code very unstable imo. I mean, yeah, that's not good to use '\' for multiline strings.

    So, break the string up into sub strings, seperated by +.

    Like so:

    "<div id='Display' style='position:relative;width:100%;'>" +
    "<img alt='Faces' src='im..." +

    etc.

    Also, you are putting a comment that says "Rewrite the contents with the following values" Which leads me to believe, you want to set the contents of the next innerHTML line. You are not doing that, you are using == to compare the innerHTML element value with that of the string on the other side of the == operator. To assign in javascript, just use one equal sign.

    foo = "bar"; // foo is now set to "bar"
    foo == "bar"; // (loose-equals) the answer is true, because the value of foo is equal to "bar".
    foo === "bar"; // (equals) the answer is true, because the value of foo is the same as "bar".

    So you clearly (to my understanding) meant to use the "=" operator out of the three possibilities above.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles