Hello guys, I have a very simple problem for you to solve. It's not simple for me but it's probably very easy to you guys. I've tried searching this but failed.
Anyway, here is my problem. I have an image. When I call a function with onclick, I pass the image's id. My problem is that when I click the image, it displays "undefined". It is supposed to display the value of the image. It works perfectly fine in internet explorer. Its firefox that's giving me the problem. Can you guys give me the code for firefox so I can adjust? Please tell me why it won't work also. kthx

html code:

<img src="Lighthouse.jpg" class="card" id="card1" value="50" onclick="reveal('card1')">

javascript code

function reveal(id)
{
    card=document.getElementById(id);
	
	alert(card.value);
}

Recommended Answers

All 2 Replies

You can simplify the onclick by passing this (which references the triggering element). Then use the getAttribute("value") on that element.

<img src="Lighthouse.jpg" class="card" id="card1" value="50" onclick="reveal(this)" />
function reveal(card)
{
    var value = card.getAttribute("value");
    alert(value);
}

Images don't really have a value so you need to use Get Attribute instead which will grab any attribute you place on an element (it shouldn't work in IE either but that's IE).

thanks. It worked perfectly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.