473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

simple onclick function

Hi Group of helpers,

Following snippet of code:

<HTML>
<HEAD>
<script>
function color(value)
{
alert(value);
}
</script>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD onclick="javascript:color('headerbackground')">
<A HREF="javascript:color('header')">Messageheader</A>
</TD>
</TR>
</BODY>
</HTML>

I would like to accomplish that when I click "Messageheader", my alert would
read: 'header'.
It does this fine, but when I OK the alert, it pops up again with:
'headerbackground'

Is there a way to prevent this?

Tha!

Mich
Dec 16 '05 #1
5 2260
uhm.
Actually, the 'headerbgcolor' pops before 'header', which is completely
wrong.
How do I pop only 'header' when clicking on that link?

"Michel" <no@spam.please> wrote in message
news:dn**********@news.cistron.nl...
Hi Group of helpers,

Following snippet of code:

<HTML>
<HEAD>
<script>
function color(value)
{
alert(value);
}
</script>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD onclick="javascript:color('headerbackground')">
<A HREF="javascript:color('header')">Messageheader</A>
</TD>
</TR>
</BODY>
</HTML>

I would like to accomplish that when I click "Messageheader", my alert would read: 'header'.
It does this fine, but when I OK the alert, it pops up again with:
'headerbackground'

Is there a way to prevent this?

Tha!

Mich

Dec 16 '05 #2

"Lee" <RE**************@cox.net> wrote in message
news:dn********@drn.newsguy.com...
Michel said:

uhm.
Actually, the 'headerbgcolor' pops before 'header', which is completely
wrong.
How do I pop only 'header' when clicking on that link?


Please don't top-post in this newsgroup.
In your example, there is no reason to have a link at all.
If this doesn't do what you want, you'll need to be more
specific.

<HTML>
<HEAD>
<script type="text/javascript">
function color(value)
{
alert(value);
}
</script>
</HEAD>
<BODY>
<TABLE>
<TR>
<TD onclick="color('header')">
Messageheader
</TD>
</TR>
</TABLE>
</BODY>
</HTML>


Hi Lee, sorry for my top-posting.
Anyway, I need the link as I want 2 seperate alerts. One for when I click
the background and one for the message itself.
Is that possible?
Dec 16 '05 #3
Michel wrote:
[...]

Hi Lee, sorry for my top-posting.
Anyway, I need the link as I want 2 seperate alerts. One for when I click
the background and one for the message itself.
Is that possible?


Yes. There are a couple of ways to do it, below is one. You can use an
onclick on the TD and then use the event properties to see what was
clicked on, or you can put an onclick on each element and go from there.

If you want to stop navigation for some reason, then you need an onclick
on the A element that returns false (or true for navigation to occur).
I've returned false so that the link isn't followed, the value returned
(either true or false) can be based on some condition.

To stop both events firing when the link is clicked, use stopPropagation
(W3C event model) and cancelBubble (IE event model).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Click play</title>
<script type="text/javascript">
function color(e, value)
{
var e = e || window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
alert(value);
return false;
}
</script>
</head>
<body>
<table>
<tr>
<td onclick="color(event, 'headerbackground');"
style="width: 10em; border:1px solid blue;">
<a href="http://www.w3.org/"
onclick="return color(event, 'header');">W3.org</a>
</td>
</tr>
</table>
</body>
</html>

--
Rob
Dec 16 '05 #4

"RobG" <rg***@iinet.net.au> wrote in message
news:43**********************@per-qv1-newsreader-01.iinet.net.au...
Michel wrote:
[...]

Hi Lee, sorry for my top-posting.
Anyway, I need the link as I want 2 seperate alerts. One for when I click the background and one for the message itself.
Is that possible?


Yes. There are a couple of ways to do it, below is one. You can use an
onclick on the TD and then use the event properties to see what was
clicked on, or you can put an onclick on each element and go from there.

If you want to stop navigation for some reason, then you need an onclick
on the A element that returns false (or true for navigation to occur).
I've returned false so that the link isn't followed, the value returned
(either true or false) can be based on some condition.

To stop both events firing when the link is clicked, use stopPropagation
(W3C event model) and cancelBubble (IE event model).
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Click play</title>
<script type="text/javascript">
function color(e, value)
{
var e = e || window.event;
e.cancelBubble = true;
if (e.stopPropagation) e.stopPropagation();
alert(value);
return false;
}
</script>
</head>
<body>
<table>
<tr>
<td onclick="color(event, 'headerbackground');"
style="width: 10em; border:1px solid blue;">
<a href="http://www.w3.org/"
onclick="return color(event, 'header');">W3.org</a>
</td>
</tr>
</table>
</body>
</html>

--
Rob


Thanks Rob. This is exactly what I wanted.

Mich
Dec 17 '05 #5
On 2005-12-16, Michel <no@spam.please> wrote:
Hi Lee, sorry for my top-posting.
Anyway, I need the link as I want 2 seperate alerts. One for when I click
the background and one for the message itself.
Is that possible?


yes, have the click event for the container check where wether the click was
on the contained item or not.

implementation is somewhat messay as different browsers identify the item
under the mouse pointer in different ways.

Bye.
Jasen
Dec 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
9
by: Astra | last post by:
Hi everybody Wonder if you could help me out. I created a simple JavaScript routine to enable a user to click backwards and forwards between small news articles. This routine works fine in IE...
0
by: 42 | last post by:
I implemented a simple class inherited from Page to create a page template. It simply wraps some trivial html around the inherited page, and puts the inherited page into a form. The problem I...
24
by: firstcustomer | last post by:
Hi, Firstly, I know NOTHING about Javascript I'm afraid, so I'm hoping that someone will be able to point me to a ready-made solution to my problem! A friend of mine (honest!) is wanting to...
3
by: manleytim | last post by:
Hello, I am pretty new to JavaScript. I was trying one of the examples in my Text Book JavaScript Third Edition and for some reason I can't seem to get the windows alert popups to say if the...
1
by: Mike Bahr | last post by:
Hi All, Im not very well versed in javascript at all so hoping someone can help me out here. I have a page that uses pairs of radio buttons to toggle visibility of some table columns. I need...
7
by: WSPL5 | last post by:
I have done this function before and I cannot figure out why it is not working. I am receiving the results of an xmlhttp call and I am looping through the values I am creating links with an OnClick. ...
15
by: Inny | last post by:
Hello, I found this simple js star rating script that I want to modify slightly. firstly I want to retain current vote , say 3 stars, untill its changed again. right now it resets to unvoted...
2
by: stevemtno | last post by:
I've got a problem with a web page I'm working on. I have 4 modules - one of them has 2 tabs, two of them have 4 tabs. When the user clicks on the tabs, the content below them changes. However, when...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.