SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    basic variable question

    Hi all, here's a simple question from a js newbie.

    I'm trying to pass a perl variable ($number) in to a javascript routine.

    Everything works fine, except I don't know how to get the js variable "thenumber" to work in this statement:

    thenumber.src = "reply.png"

    The variable "thenumber" is not printing the value it contains.

    If I print it out, it works: document.write(thenumber)

    Thanks for your patience with what I'm sure is a very basic question!



    <SCRIPT LANGUAGE=JavaScript>
    intImage = 2;
    thenumber = "$number";
    function swapImage() {
    switch (intImage) {
    case 1:
    thenumber.src = "reply.png"
    intImage = 2
    return(false);
    case 2:
    thenumber.src = "close-panel.png"
    intImage = 1
    return(false);
    }
    }
    </SCRIPT>



    <img id="$number" name="$number" src = "reply.png" onclick="swapImage();">
    </a>

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2003
    Location
    USA
    Posts
    2,582
    Mentioned
    29 Post(s)
    Tagged
    0 Thread(s)
    I don't see any particular problems with it.

    I do wonder though, why are you returning false? I don't see any particular need to return that from the code you have given.

    EDIT: Actually, I do see a possible problem.

    Instead of using:
    Code:
    thenumber = "$number";
    Try:
    Code:
    thenumber = document.getElementById("$number");

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Many thanks for your reply. Like you said, the js is not the problem.

    I am looping over this code with Perl, to create Reply buttons in a forum.

    When I increment a number to ID each image, the js fails. BUT, when I increment a letter, it works.

    I'm not sure why this is, but for the moment I'm guessing this is really a Perl question, not a javascript question.

    Sorry, I don't know about the returning false. I didn't write the js code, and as you can see, don't know much about js.

    Appreciate your reply!

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML ID attributes may not begin with a number.


Bookmarks

Posting Permissions

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