Web Analytics Made Easy -
StatCounter html get id of element when you press button - CodingForum

Announcement

Collapse
No announcement yet.

html get id of element when you press button

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Just pass the this object reference, like
    Code:
    onclick="showpic(this);"
    Code:
    showpic(obj){
    alert(obj.getAttribute('id') );
    }
    Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

    Comment


    • #3
      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

      Comment


      • #4
        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?
        Digitalocean Cloud Hosting (Referral link - get $10 free credit) Fameco

        Comment


        • #5
          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

          Comment

          Working...
          X