954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get Id Of an elment in onClick event

Hi Frendz,

How to get the id of an element using onClick Event in tag using javascript?

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

more detail needed
exactly what are you trying to accomplish, is there a single element, or do you want to know which of a number of elements is clicked
what code have you already
fixing code is a lot easier than writing code, so post what you have between [code=html] codes [/code] tags

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 
element.id
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
 

Just elaborating on twiss's post:

<script type="text/javascript">
function doSomething(elem) {
alert ('The ID of the element which triggered this is: ' + elem.id;
}
</script>
<div id="blah" onClick="doSomething(this)">Blah blah blah</div>
edwinhermann
Junior Poster
141 posts since Sep 2009
Reputation Points: 67
Solved Threads: 29
 
more detail needed exactly what are you trying to accomplish, is there a single element, or do you want to know which of a number of elements is clicked what code have you already fixing code is a lot easier than writing code, so post what you have between [code=html] codes [/code] tags

This is one of my div in my page

<div class="signin_center"><a href="#" name="0" id="signin" onclick="twitterLogin()">Sign in</a> &nbsp; Register</div>


and this is my script

function twitterLogin()
{
	value = document.getElementById("signin").name;
	//alert(value);
	if(value==0)
	{
		document.getElementById("signin").name=1;
		document.getElementById("twitter").style.display='block';
	}
	if(value==1)
	{
		document.getElementById("signin").name=0;
		document.getElementById("twitter").style.display='none';
	}
}


I have Login div with id "twitter". I need to open this div when clicking the sign in. Its working fine when click the sign in link. But now i need if the users click other than the sign in link then the login div should be closed.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

One way is to have a transparent DIV that takes up the whole screen with a high z-index value, but still lower than the sign-in DIV. On that DIV you'd have something like this:

<div onClick="cancelSignIn()">


And the corresponding Javascript:

function cancelSignIn() {
document.getElementById("signin").name=0;
document.getElementById("twitter").style.display='none';
}


A full-page DIV an be achieved by putting it after the tag and applying the following CSS (assuming the DIV's id is "canceltwitter":

div#canceltwitter {
position: absolute;
margin: 0px;
padding: 0px;
border: 0px;
width: 100%;
height: 100%;
z-index: 50;
display: none;
}


You'd need to also set the z-index of the Twitter div to something greater than 50, and use javascript to set the "display" property of the canceltwitter div to block at the same time as displaying the twitter div. (Also putting the canceltwitter div's display property back to none as part of the cancelSignIn() function).

edwinhermann
Junior Poster
141 posts since Sep 2009
Reputation Points: 67
Solved Threads: 29
 

One way is to have a transparent DIV that takes up the whole screen with a high z-index value, but still lower than the sign-in DIV. On that DIV you'd have something like this:

<div onClick="cancelSignIn()">

And the corresponding Javascript:

function cancelSignIn() {
document.getElementById("signin").name=0;
document.getElementById("twitter").style.display='none';
}

A full-page DIV an be achieved by putting it after the tag and applying the following CSS (assuming the DIV's id is "canceltwitter":

div#canceltwitter {
position: absolute;
margin: 0px;
padding: 0px;
border: 0px;
width: 100%;
height: 100%;
z-index: 50;
display: none;
}

You'd need to also set the z-index of the Twitter div to something greater than 50, and use javascript to set the "display" property of the canceltwitter div to block at the same time as displaying the twitter div. (Also putting the canceltwitter div's display property back to none as part of the cancelSignIn() function).


Ok Thanks! I'll try this.

Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

+ could check the innertext or innerhtml of the twitter signin for twitter.com's valid login text to close the divs on receipt of login confirmation
+ should include a button in the twitter div labelled close that fires the CancelSiginin() script, or label the background div, unless there is a visible control some users may refresh the page instead of looking for a close device, and lose whatever they have previously input
& checkout the twitter helpscreens, they probably have this issue pretty well covered

almostbob
Posting Sensei
3,149 posts since Jan 2009
Reputation Points: 571
Solved Threads: 376
 

Thanks to all.... I have got solution using the following codes.

function twitterLogin(event)
{
	if(event.target.id=="signin" || event.target.id=="twitter")
	{
		value = document.getElementById("signin").name;
		if(value==0)
		{
			if(event.target.id=="signin")
			{
				document.getElementById("signin").name=1;
				document.getElementById("twitter").style.display='block';
			}
		}
		if(value==1)
		{
			if(event.target.id=="signin")
			{
				document.getElementById("signin").name=0;
				document.getElementById("twitter").style.display='none';
			}
		}
	}
	else
	{
		document.getElementById("signin").name=0;
		document.getElementById("twitter").style.display='none';
	}
}
<body onclick="twitterLogin(event)">
    
<div id="twitter" class="twiter_login" style="display:none;">
<!-- Login Design -->
</div>

<a href="#" name="0" id="signin">Sign in</a>
Karthik_pranas
Posting Pro
564 posts since Feb 2011
Reputation Points: 96
Solved Threads: 97
 

Thank u karthik. It is really very useful to us. this is good solution. NW i am using this script for my project also. Thank you very much

ushajase
Newbie Poster
18 posts since Jun 2011
Reputation Points: 21
Solved Threads: 1
 

Thank u karthik. It is really very useful to us. this is good solution. NW i am using this script for my project also. Thank you very much

ushajase
Newbie Poster
18 posts since Jun 2011
Reputation Points: 21
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: