newbie: insertBefore, Invalid Argument error

Discussion in 'Javascript' started by Jeff, Jan 26, 2007.

  1. Jeff

    Jeff Guest

    Hey

    I'm trying to use javascript to dynamically add text to the web page. It
    should work like this: The user tryes to click on a link in the web page.
    The link executes a javascript method, which checks if a hidden html field
    has a value. If that hidden field has no value then the javascript should
    display a message (not using alert, but insert a html element into the code)
    on the web page telling the user that he need to save the form before
    clicking on that link..

    Below is the approach I've tryed to solve this by... I just posted the code
    I have problem with... the check if the hidden field has a value etc is
    working okay, so I didn't post it... if you need to see that code too, just
    tell me and I'll post that too.

    This javascript gives an Invalid Argument error

    This is the html code the javascript below are trying to insert an element
    before
    <div id="error"></div>

    var oNewP = document.createElement("div");
    var oText = document.createTextNode("Hello World");
    oNewP.appendChild(oText);
    var v1 = document.getElementById("error");
    document.body.insertBefore(oNewP, v1);

    Any suggestions?

    Best Regards

    Jeff
     
    Jeff, Jan 26, 2007
    #1
    1. Advertising

  2. Jeff

    RobG Guest

    On Jan 26, 2:17 pm, "Jeff" <> wrote:
    > Hey
    >
    > I'm trying to use javascript to dynamically add text to the web page. It
    > should work like this: The user tryes to click on a link in the web page.
    > The link executes a javascript method, which checks if a hidden html field
    > has a value. If that hidden field has no value then the javascript should
    > display a message (not using alert, but insert a html element into the code)
    > on the web page telling the user that he need to save the form before
    > clicking on that link..
    >
    > Below is the approach I've tryed to solve this by... I just posted the code
    > I have problem with... the check if the hidden field has a value etc is
    > working okay, so I didn't post it... if you need to see that code too, just
    > tell me and I'll post that too.
    >
    > This javascript gives an Invalid Argument error
    >
    > This is the html code the javascript below are trying to insert an element
    > before
    > <div id="error"></div>
    >
    > var oNewP = document.createElement("div");
    > var oText = document.createTextNode("Hello World");
    > oNewP.appendChild(oText);
    > var v1 = document.getElementById("error");
    > document.body.insertBefore(oNewP, v1);
    >
    > Any suggestions?


    v1 probably isn't a direct child of the body. Try:

    v1.parentNode.insertBefore(oNewP, v1);


    --
    Rob
     
    RobG, Jan 26, 2007
    #2
    1. Advertising

  3. Jeff

    Jeff Guest

    "RobG" <> wrote in message
    news:...
    >
    >
    > On Jan 26, 2:17 pm, "Jeff" <> wrote:
    >> Hey
    >>
    >> I'm trying to use javascript to dynamically add text to the web page. It
    >> should work like this: The user tryes to click on a link in the web page.
    >> The link executes a javascript method, which checks if a hidden html
    >> field
    >> has a value. If that hidden field has no value then the javascript should
    >> display a message (not using alert, but insert a html element into the
    >> code)
    >> on the web page telling the user that he need to save the form before
    >> clicking on that link..
    >>
    >> Below is the approach I've tryed to solve this by... I just posted the
    >> code
    >> I have problem with... the check if the hidden field has a value etc is
    >> working okay, so I didn't post it... if you need to see that code too,
    >> just
    >> tell me and I'll post that too.
    >>
    >> This javascript gives an Invalid Argument error
    >>
    >> This is the html code the javascript below are trying to insert an
    >> element
    >> before
    >> <div id="error"></div>
    >>
    >> var oNewP = document.createElement("div");
    >> var oText = document.createTextNode("Hello World");
    >> oNewP.appendChild(oText);
    >> var v1 = document.getElementById("error");
    >> document.body.insertBefore(oNewP, v1);
    >>
    >> Any suggestions?

    >
    > v1 probably isn't a direct child of the body. Try:
    >
    > v1.parentNode.insertBefore(oNewP, v1);
    >
    >
    > --
    > Rob
    >


    Thanks, now the "Hello World" is displayed when this code is executed. but
    just one more thing.. I want the Hello World to be displayed with red text
    centered on the screen also:
    var oNewP = document.createElement('div style="color:red;
    text-align:center;"');
    var oText = document.createTextNode("Hello World");
    oNewP.appendChild(oText);
    var v1 = document.getElementById("error");
    v1.parentNode.insertBefore(oNewP, v1);

    But this code generate this error:
    "Unexpected call to method or property access", in my opinion that error is
    related to
    var oNewP = document.createElement('div style="color:red;
    text-align:center;"');

    Any suggestions?
     
    Jeff, Jan 26, 2007
    #3
  4. Jeff

    Toby Inkster Guest

    Jeff wrote:

    > var oNewP = document.createElement('div style="color:red;
    > text-align:center;"');


    var oNewP = document.createElement('div');
    oNewP.style.color = 'red';
    oNewP.style.textAlign = 'center';

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
     
    Toby Inkster, Jan 27, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Achille

    URGENT: InsertBefore

    Achille, Nov 3, 2004, in forum: Java
    Replies:
    1
    Views:
    422
    Martin Honnen
    Nov 3, 2004
  2. Horny Porno-thologist

    Can't seen to use insertBefore

    Horny Porno-thologist, Jan 28, 2004, in forum: XML
    Replies:
    0
    Views:
    371
    Horny Porno-thologist
    Jan 28, 2004
  3. NotGiven
    Replies:
    0
    Views:
    525
    NotGiven
    Sep 17, 2004
  4. Java Guy
    Replies:
    1
    Views:
    714
    Manish Pandit
    Oct 15, 2006
  5. Java Guy
    Replies:
    1
    Views:
    336
Loading...

Share This Page