Solved

Using onDblClick with link

Posted on 1998-11-26
6
240 Views
Last Modified: 2012-06-21
I would like to make a link perform 2 different functions.
One for single click and another for double click. When
I try it with a link, it only performs the single click function and when I move the cursor, the text on the page becomes highlighted. Any suggestions?
0
Comment
Question by:steve44
6 Comments
 

Expert Comment

by:burtons2
ID: 1274635
Hi,
I'm not sure about this for definate, but I think JavaScript only has an onClick event, not an onDblClick (as DHTML has).
The way I would suggest doing it using JavaScript is put a timeout on your function, then if the event it triggered once during the timeout interval, do this, if triggered twice during the interval, do that!

Hope this is of help!
0
 
LVL 2

Expert Comment

by:Grdv
ID: 1274636
place this in the head:
<script language="JavaScript">
<!--
var clicked=null;
fucntion oneClick(){
//your click operation
}
fucntion oneClick(){
//your dblclick operation
}
//-->
</script>

and then do place this on the link you want to change
<a href="javascript:if(clicked==null)clicked=setTimeout("oneClick()",400);else{clearTimout('clicked'); twoClicks();}">whatever link description</a>

think that's about it... I might have typed wrong... but it should be right

//grdv
0
 

Expert Comment

by:th_c_man
ID: 1274637
instead of using a <A HREF....> You could use a
<FORM><Input Type="button" VALUE="CLick Here" onCLick="buttonClicked();>"</FORM>
<SCRIPT>
function buttonClicked(){
//your function(s)
}

and use the timeout thingy suggested by grdv,
or instead of the <FORM><INPUT> use an <IMG>, this will stop browsers from just following the link if they have javascript turned off.

I dunno, give it a try...

Daniel
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 100 total points
ID: 1274638
Very shabby, grdv!

steve: What you are trying is in my opinion very unintuitive and will confuse the user. If you still insist, here are two versions - the first is a working version of grdv's script - this actually works quite well.

The second is to show how it could have worked but since onClick is triggered when the user double clicks it does not. To make that work, one would need to count onMousedowns and ups.

<HTML>
<script language="JavaScript"><!--
function myVoid() { ; } // nothing

var clicked=null;
function oneClick(){ // oneclick operation
   alert('one');
   clicked=null;
}
function twoClicks(){ // dblclick operation
   alert('two');
   clicked=null;
}
//--></script>

<a href="javascript:myVoid()"
onClick="
if(clicked==null) clicked=setTimeout('oneClick()',400);
else {
   clearTimeout(clicked);
   twoClicks();
}">Click or doubleclick this</a>

<P><a href="javascript:myVoid(0)"
onClick="document.forms[0].elements[0].value='one'"
onDblClick="document.forms[0].elements[1].value='two'"
>onClick and on dblclick</a>
<FORM>
<INPUT TYPE="TEXT">
<INPUT TYPE="TEXT">
</FORM>
</HTML>

I think grdv and I both deserve points. I will answer, please accept and ask Linda in Customer services to award some points to give in a question just for grdv.

Michel
0
 
LVL 1

Expert Comment

by:akma43
ID: 9074548
But this still executes the single click operation. Can anyone do only  dblclick and not singleclick??
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 9101525
Please see the date.
OLD browsers.

Here

<a href="#"
onClick="document.forms[0].elements[0].value='one';
return false"
onDblClick="document.forms[0].elements[1].value='two';
return false"
>onClick and on dblclick</a>
<P><a href="#"
onDblClick="document.forms[0].elements[1].value='two';
return false"
>ONLY dblclick</a>
<FORM>
<INPUT TYPE="TEXT">
<INPUT TYPE="TEXT">
</FORM>
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTTPS jquery doesn't work 9 58
How can I get this SlideToggle to behave? 6 23
CONVERT JAVASCRIPT BUTTON TO HYPERLINK TEXT 6 25
asp.net mvc5 6 12
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now