Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    html get id of element when you press button

    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
    Code:
    id="btnwebsite"
    now in the onclick it has:
    Code:
    onclick="showpic(id);"
    now would this actually work or do you HAVE to use:
    Code:
    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.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Just pass the this object reference, like
    Code:
    onclick="showpic(this);"
    Code:
    showpic(obj){
    alert(obj.getAttribute('id') );
    }
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    heres my showpic script

    Code:
    <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

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,865
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    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?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

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