-
March 29th, 2007, 06:00 PM
#1
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.
-
March 29th, 2007, 06:13 PM
#2
Re: Using variables in Javascript
Did you try
Code:
a.innerHTML = "<img src='folder2/" + img + "' />";
?
- petter
-
March 30th, 2007, 06:41 AM
#3
Re: Using variables in Javascript
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|