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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onClick event within a DIV tag

Status
Not open for further replies.

rrhandle

Programmer
Dec 26, 2001
193
US
I would like to trigger the Javascript onClick event when the user clicks anywhere within a <DIV> tag below. Is that possible?

response.write("<DIV STYLE='position:absolute; top:250px; left:40px; width:400px; height:300px; background-color:blue; color:red; background: url(mmc.gif);'></DIV>"

Thanks

 
I was trying to call the alert function right from the DIV tag, but could not get it to work, so I created a function name "divClick", and called it from the DIV tag. That works.

Add this piece of Javascript.
Code:
<script type="text/javascript">
<!--
function divClick()
{
	alert('div onClick');
}

function linkClick()
{
  var div = document.getElementById("wrapper");
  div.onclick = null;
	alert('anchor onClick');
}
// -->
</script>

Then add onClick='divClick();' to the DIV tag.

Code:
response.write("<DIV onClick='divClick();' STYLE='position:absolute; top:250px; left:40px; width:400px; height:300px; background-color:blue; color:red; background: url(mmc.gif);'></DIV>")

 
Have you tried it yet?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
LFI: Yes, it is working.

Juris: Isn't that basically what I did?

 
rrhandle,

I hadn't seen your second post when I posted my response above.

I'm guessing you had trouble with the alert written INSIDE the DIV tag because of nested quotes (the text in your alert was probably wrapped in single-quotes, as is the argument of the onclick event... a javascript no-no).

So, you have it working and you have your answer, yes?

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top