Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return tagName 2

Status
Not open for further replies.

MarkZK

Technical User
Jul 13, 2006
202
GB
Hi all,

Without adding onclick events to all my html tags, is there a way to alert the tagname of each tag clicked ?, or maybe if there's a way to add onclick to the body tag and then check which tag was clicked through that function. ?

Thanks.
 
Thanks to the fact that events will bubble up, you can do as you suspect:

Code:
<html>
<head>
	<script type="text/javascript">

		window.onload = setupFunc;
		
		function setupFunc() {
			document.getElementsByTagName('body')[0].onclick = clickFunc;
		}
		
		function clickFunc(eventData) {
			var clickedElement = (window.event) ? event.srcElement : eventData.target;
			alert('You clicked the element with a tag name of:\n\n' + clickedElement.tagName + '\n\nand an ID of:\n\n' + clickedElement.id);
		}
		
	</script>
</head>

<body>
	<div id="wibble">
		<span id="someOther1">
			Click
		</span>
		<span id="someOther2">
			Me
		</span>
	</div>
</body>
</html>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Dan,
Thank's that's great. is it cross browser compliant?

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top