SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast LobsterMan's Avatar
    Join Date
    Apr 2005
    Location
    Jerusalem, Israel
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Highlight HTML element on click

    Hi,
    I would like to write a script that will highlight any html element in a page (or maybe just divs) onclick, and print out the name of that element on the top of the screen somehow. I cannot make any major adjustments to this html, just insert a script because this will need to work for user submitted html pages.

    I'm no javascript expert. Any direction and advice would be appreciated. Tanks in advance.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Enthusiast LobsterMan's Avatar
    Join Date
    Apr 2005
    Location
    Jerusalem, Israel
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, that's great! thanks.
    Just one question, I see that that script uses tagName to get the tags name. is there something i can use to get the tag's id and class?

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,608
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    getElementById()

    getElementsByClassName()
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Enthusiast LobsterMan's Avatar
    Join Date
    Apr 2005
    Location
    Jerusalem, Israel
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as my limited knowledge of javascript goes, this will select an element if I input the id. i need the opposite, get id of selected element. is this possible?

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure,
    var elm = document.getElementById('some-id');
    alert(elm.getAttribute('id'));

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the example code
    targ.id
    targ.className

  8. #8
    SitePoint Enthusiast LobsterMan's Avatar
    Join Date
    Apr 2005
    Location
    Jerusalem, Israel
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    in the example code
    targ.id
    targ.className
    Thats exactly what I was looking for! thanks!


Bookmarks

Posting Permissions

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