How to pass variable in a string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolchippy
    New Member
    • Jul 2006
    • 5

    How to pass variable in a string

    I actually want to pass a VARIABLE(that holds the name of the img tag) in
    document.VARIAB LE.src=imga
    when i try using document.arg.sr c=imga, there is an error
    but when i see the value of arg it contains the name of the img tag

    How can i correct this or is there any other way to do it, If anyone can do this by DOM it would be great

    [HTML]<html>
    <head><TITLE>Un iversal Engineers</TITLE>
    <script language="javas cript">
    function over(arg)
    {
    imga=new Image()
    imga="images/bt2.gif"
    document.n.src= imga
    }
    function out(arg1)
    {
    imgb=new Image()
    imgb="images/rivet.jpg"
    document.n.src= imgb
    }
    </script>
    </head>
    <body bgcolor=white>
    <center>
    <IMG SRC="images/ue.gif" border=0><br>
    <IMG SRC="images/namaste1.bmp" border=0>
    <table border=0>
    <tr>
    <td><IMG SRC="images/rivet.jpg" border=0 name=n alt="Profile" onMouseOver=ove r(this.name) onMouseOut=out( this.name)>
    <td>
    <td><IMG SRC="images/rivet.jpg" border=0 name=m alt="Flow" onMouseOver=ove r(this.name) onMouseOut=out( )>
    <td>
    <td><IMG SRC="images/rivet.jpg" border=0 name=o alt="Level" onMouseOver=ove r(this.name) onMouseOut=out( )>
    <td>
    <td><IMG SRC="images/rivet.jpg" border=0 name=p alt="Mass" onMouseOver=ove r(this.name) onMouseOut=out( )>
    </tr>
    </table>
    </center>
    </BODY>
    </HTML>[/HTML]
    Last edited by acoder; May 11 '07, 11:17 AM. Reason: Code in tags
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    There are a couple of problems with your code.

    Like the fact that you didn't use code tags when you posted your source.

    Comment

    • coolchippy
      New Member
      • Jul 2006
      • 5

      #3
      Actually I did not get you,

      How can I pass a varaible in a document.name.s rc=imga in place of name

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        Originally posted by coolchippy
        Actually I did not get you,

        How can I pass a varaible in a document.name.s rc=imga in place of name
        pbmods wants to say that you should wrap your code-examples in CODE tags ... that makes it easier to read for everyone ...

        an easy way to achive your goal would be the following example:

        Code:
        // prefer to use id in your image_tags and pass that to function over
        // over_images maps that ids to the desired image-sources
        
        var over_images = {
            m: 'desired_imagesrc.ext',
            n: 'desired_imagesrc1.ext',
            o: 'desired_imagesrc2.ext',
            p: 'desired_imagesrc3.ext'
        };
        
        function over(id) {
            // gives you a ref to the img-node
            var img = document.getElementById(id);
        
            img.setAttribute('src', over_images[id]); 
        };
        hope it helps ...

        Comment

        Working...