...

View Full Version : html get id of element when you press button



3dkingpin
04-11-2009, 02:04 PM
Firstly Hi to all, im new here and getting back into html etc.. so il asking alot of questions (maybe).

so here goes.

I have a button on the page it has where

id="btnwebsite"

now in the onclick it has:

onclick="showpic(id);"

now would this actually work or do you HAVE to use:

onclick="showpic('btnwebsite');"

basically i want to send the id of the button pressed to the showpic javascript function. it would be easier to use showpic(id) as i can then simply copy the code to all other buttons.

abduraooft
04-11-2009, 02:10 PM
Just pass the this object reference, like

onclick="showpic(this);"


showpic(obj){
alert(obj.getAttribute('id') );
}

3dkingpin
04-11-2009, 02:17 PM
heres my showpic script


<SCRIPT language="" type=text/javascript>
function showpic(myid)
{
alert(myid.getAttribute("id");
if (myid="btnwebsite") {websiteimg.style.display=""};
else {websiteimg.style.display="none"};
}</SCRIPT>

but when i test page it says scripting error

abduraooft
04-11-2009, 02:45 PM
basically i want to send the id of the button pressed to the showpic javascript function. it would be easier to use showpic(id) as i can then simply copy the code to all other buttons. Are you trying to hide that button itself or another element like an img? Are you using the same id for more than one element? Could you post your html code?

3dkingpin
04-11-2009, 03:18 PM
the button is called btnwebsite when i press this button i want the picture called websiteimg to show. when another button is pressed i want the websiteimg to not be visible



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum