Dynamic text: modify text by image/link click

Discussion in 'Javascript' started by David Housman, Feb 2, 2007.

  1. Hello,

    The effect i'm trying for:
    Tha tthe user clicks an image, and a line of text on the page changes.
    I'd like to make it so that this could be done over and over with
    different links.
    But this isn't working. I think the problem is related to a refresh
    not happening, but I'm just learning the javascript ropes.
    Thanks in advance for your help.

    -Dave
    --code follows

    *attempt*1*
    var avar = 'beforeclick';
    //defined in body
    //included document.write(avar) in text. it shows up in text, but
    doesn't change on click.

    function newtext(){
    document.write(avar);
    return false;
    }
    <!-- setInterval("newtext()",500);-->
    //page doesn't finish loading
    newtext();
    /this is included in the body of the page.

    <a onClick="avar='afterclick'; newtext(); return false;">first link</
    a>
    //results in a new page with the only element "afterclick"
    <a onClick="avar='afterclick'; return false;">first link</a>
    //no effect

    *Attempt*2*: I also try doing this as an anchor, rather than a global
    var.
    <a name = "anchor">This is the original text</a>

    <a onClick="document.getelementbyid("anchor")='newtext?'; return
    false;">anchorlink</a>
    //no effect
    <a onClick="anchor='newtext?'; return false;">anchorlink</a>
    //no effect
     
    David Housman, Feb 2, 2007
    #1
    1. Advertising

  2. David  Housman

    marss Guest

    David Housman wrote:

    > The effect i'm trying for:
    > Tha tthe user clicks an image, and a line of text on the page changes.
    > I'd like to make it so that this could be done over and over with
    > different links.


    > *Attempt*2*: I also try doing this as an anchor, rather than a global
    > var.
    > <a name = "anchor">This is the original text</a>
    >
    > <a onClick="document.getelementbyid("anchor")='newtext?'; return
    > false;">anchorlink</a>
    > //no effect
    > <a onClick="anchor='newtext?'; return false;">anchorlink</a>
    > //no effect


    Hi,
    Javascript is case sensitive that is why "getelementbyid" not the same
    that "getElementById".
    If you want to search by id (getElementById) then set id (not name).
    <span id="anchor1">This is the original text</span>
    <a href="#"
    onclick="document.getElementById('anchor1').innerHTML='newtext?';
    return false;">anchorlink</a>

    or if you want the change occurs after the user clicks on an image:
    <span id="anchor1">This is the original text</span>
    <img src="someimg.gif"
    onclick="document.getElementById('anchor1').innerHTML='newtext?'">
     
    marss, Feb 2, 2007
    #2
    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. Purvi T
    Replies:
    0
    Views:
    858
    Purvi T
    Oct 19, 2004
  2. Kevin Spencer

    Re: Link Link Link DANGER WILL ROBINSON!!!

    Kevin Spencer, May 17, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    865
    Kevin Spencer
    May 17, 2005
  3. Replies:
    0
    Views:
    112
  4. Blue®
    Replies:
    4
    Views:
    813
    Blue®
    Sep 27, 2003
  5. Peter Michaux

    Synthetic click on link doesn't follow link

    Peter Michaux, Jun 4, 2007, in forum: Javascript
    Replies:
    2
    Views:
    178
    -Lost
    Jun 5, 2007
Loading...

Share This Page