CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2005
    Posts
    6

    Using variables in Javascript

    I usually do PHP programming, but I need to get a Javascript functions working with variables.

    This is the original code that's working:
    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
    function show_comment(id, a) {
    
    	menu = document.getElementById(id);
    
    	if(menu.style.display == "block"){
    		menu.style.display = "none";
    		a.innerHTML = "<img src='folder1/a.gif' />";
    	} else {
    		menu.style.display = "block";
    		a.innerHTML = "<img src='folder2/a.gif' />";
    	}
    
    }
    
    </script>
    
    </head>
    
    <body>
    
    <a href="#" onclick="show_comment('1', this);return false;">
    	<img src='folder2/a.gif' />
    </a>
    
    <div style="display:block;" id="1"><h1>HAAAJ</h1></div>
    
    </body>
    </html>

    I would now like to change the function so I can use it with different images and still use the same function.

    My idea is to change:
    show_comment('1', this)
    to:
    show_comment('1', this, 'a.gif')

    And:
    function show_comment(id, a) {

    menu = document.getElementById(id);

    if(menu.style.display == "block"){
    menu.style.display = "none";
    a.innerHTML = "<img src='folder1/a.gif' />";
    } else {
    menu.style.display = "block";
    a.innerHTML = "<img src='folder2/a.gif' />";
    }

    }

    To:
    function show_comment(id, a, img) {

    menu = document.getElementById(id);

    if(menu.style.display == "block"){
    menu.style.display = "none";
    a.innerHTML = "<img src='folder1/" . img . "' />";
    } else {
    menu.style.display = "block";
    a.innerHTML = "<img src='folder2/" . img . "' />";
    }

    }

    So the new script would look like this:
    Code:
    <html>
    <head>
    
    <script type="text/javascript">
    
    function show_comment(id, a, img) {
    
    	menu = document.getElementById(id);
    
    	if(menu.style.display == "block"){
    		menu.style.display = "none";
    		a.innerHTML = "<img src='folder1/" . img . "' />";
    	} else {
    		menu.style.display = "block";
    		a.innerHTML = "<img src='folder2/" . img . "' />";
    	}
    
    }
    
    </script>
    
    </head>
    
    <body>
    
    <a href="#" onclick="show_comment('1', this, 'a.gif');return false;">
    	<img src='folder2/a.gif' />
    </a>
    
    <div style="display:block;" id="1"><h1>HAAAJ</h1></div>
    
    </body>
    </html>

    The original script is working, but not my version with that extra variable, can somebody please help me.

    Thanks in advance.

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Using variables in Javascript

    Did you try
    Code:
    a.innerHTML = "<img src='folder2/" + img + "' />";
    ?

    - petter

  3. #3
    Join Date
    Aug 2005
    Posts
    6

    Re: Using variables in Javascript

    Quote Originally Posted by wildfrog
    Did you try
    Code:
    a.innerHTML = "<img src='folder2/" + img + "' />";
    ?

    - petter

    It's working, big thanks!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured