Results 1 to 5 of 5

Thread: Detect Element ID onClick

  1. #1
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Detect Element ID onClick

    Hi

    I'm having serious difficulty with the following script concept:

    (Element: DIV / Textbox)

    I need a script that detects an element's ID during an onClick or onMouseover and passes it to a unique variable.
    Ideally, the script should be an external / global script and not be embedded within the element (all elements will be assigned an ID - The OnClick/Mouseover event can be called from the element).

    Your help will be greatly appreciated!!

    Thanks in advance,
    Kermit

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I found this a bit amusing. It would be so much easier for someone to help you if you explained what you are trying to do. The method you outline could be done (well, probably as, it is not very clear) but, it would be pointless under most circumstances. Especially the part about putting each id into a unique variable. To be of any real value (under most circumstances) one variable would be used for all the id's, only its value would change as the mouse moved over or clicked on the various elements on the page. You really should decide on mouseover or onclick as well. For a document wide script, onclick is much more feasible but, onmouseover could work, depending upon the purpose and the construction of the page in general.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default More...

    Ok - one variable and onClick is ok. Basically I'm trying to detect the ID of the element that got clicked on, then pass that to a variable. Then call a function that looks at that variable, pass it again as an ID.
    For example: Var thisID = 'ThisDiv' then in the function: document.getElementbyId('thisID').

    After that I can do virtually anything to the element that got clicked on.
    I don't want to write a function/script for each and every element; when all I need is the ID of the element that got clicked on passed to a variable, which in turn can be used within a function.

    Hope that clarifies things a bit! Anymore questions, please don't hesitate to ask.

    Thanks again!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I can work with that. Here is a demo:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    <html id="doc">
    <head>
    <title>Grab ID - Demo</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    var theId
    function grabID(e){ //modified from script at: http://www.quirksmode.org/js/events_properties.html#target
    	var targ;
    	if (!e) var e = window.event;
    	if (e.target) targ = e.target;
    	else if (e.srcElement) targ = e.srcElement;
    	if (targ.nodeType == 3) // defeat Safari bug
    		targ = targ.parentNode;
    theId=targ.id
    alert(theId) //this line may be removed or commented out
    }
    
    document.onclick=grabID;
    </script>
    </head>
    <body id="bod">
    Hi
    </body>
    </html>
    Notes: So as to not allow a situation where an element will have no id, even the html and body tags must have one as shown in this demo. Since you are really just after the element, you might want to consider using or otherwise modifying the script this was modified from at Quirksmode.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2005
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Awesome!!!!

    Hahahahahaha! It works!

    Thanks for your help!

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
  •