JavaScript to detect which mouse button is clicked

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

JavaScript to detect which mouse button is clicked

In this video, we will discuss how to use JavaScript and detect which mouse button is clicked.

Left Button 1
Middle Button 4
Right Button 2

Left Button 1
Middle Button 2
Right Button 3

Depending on the browser used, the following code returns left, middle and right click codes.

[input type="button" value="Click Me" onmouseup="getMouseClickCode(event)" /]
[input type="button" value="Clear" onclick="clearText()" /]
[br /]
[br /]
[textarea id="txtArea" rows="3" cols="10"][/textarea]

[script type="text/javascript"]
function clearText()
{
}

function getMouseClickCode(event)
{
{
}
else
{
}
}

function disableRightClick(event)
{

{
}
else
{
}
}
[/script]

The following JavaScript code detects which mouse button is clicked. It works in all versions of IE and most other W3C complaint browsers.

[script type="text/javascript"]
function whichMouseButtonClicked(event)
{
var whichButton;
{
{
case 1:
whichButton = "Left Button Clicked";
break;
case 2:
whichButton = "Middle Button Clicked";
break;
case 3:
whichButton = "Right Button Clicked";
break;
default:
whichButton = "Invalid Button Clicked";
break;
}
}
else
{
{
case 1:
whichButton = "Left Button Clicked";
break;
case 4:
whichButton = "Middle Button Clicked";
break;
case 2:
whichButton = "Right Button Clicked";
break;
default:
whichButton = "Invalid Button Clicked";
break;
}
}

alert(whichButton);
}

function disableRightClick(event)
{

{
}
else
{
}
}
[/script]
[button onmouseup="whichMouseButtonClicked(event)"]Click Me[/button]
Рекомендации по теме
Комментарии
Автор

kudvenkat your knowledge is vast..there is no boundaries or limit.Thanks for all your help.

iqbaljournals
Автор

Thanks a lot Venkat, you are superb :D

rajmahendervarma
Автор

This works for onmouseup event. But when I changed it to onclick, right-click doesn't generate 'Right button clicked'. Left and middle clicks still work. Why would that be?

ConaxHateGG
Автор

nice bro, but im looking, when u click, then the word" has next, but by list

ElmoGoesPunk
Автор

Why does visual studio show red underlines when there is no error in the code?

maheshsutar
Автор

hii.. will these code will work in normal notepad also?

priyankakumawat
Автор

I didn't understand how the computer interprets case 1 to which mouse button clicked

SHERRY
Автор

Sometimes its useful to have plugins and show where brackets ends.

pastuh
Автор

I didn't know that there's a middle-click🤔

yinadu
Автор

Their is no middle button for laptop!....😅😂

morningstar
join shbcf.ru