To allow left click only on some elements of web page NOT all elements

V

vikas.khengare

Hi friends

I want to allow left click only on some controls but not on all
controls like textbox/span elements.

I have near about 30+ HTML controls on my web page. then It's bad idea
that I will add listeners to some of them and not to all. if I want it
for >20 then I should required 20 lines for adding listeners to that
controls only.

So Is there any smart way to do that ie Except 5 comtrols I will left
click on remaining elements.

Thanks in advance
(e-mail address removed)
 
R

RobG

Hi friends

I want to allow left click only on some controls but not on all
controls like textbox/span elements.

What do you mean by 'textbox'? Is that an input type text, or a
textarea? Left-click on any of those elements does nothing by default,
you need to add an onlcick attribute for something to happen, and then
only if JavaScript is available.

I have near about 30+ HTML controls on my web page. then It's bad idea
that I will add listeners to some of them and not to all.

Why? There is no need to add listeners to all elements just because one
or two have them.

... if I want it
for >20 then I should required 20 lines for adding listeners to that
controls only.

And your problem with that is? If the code is short, say just calling a
function with one or two parameters, it is the most common way to do it.

So Is there any smart way to do that ie Except 5 comtrols I will left
click on remaining elements.

You can use script to add onclick attributes, it means that users
without JavaScript don't have useless attributes in their HTML (though
it's not all that common, most prefer to just add the code to the HTML).

It means that onclick attributes probably aren't added until after the
document has loaded, so it sometimes seems inconsistent on slow loading
sites.

You can use something like:

function someFn(){ ... }

function initOnclicks()
{
if (!document.getElementById) return;

var el;
var idArray = ['id01','id02',...];

for (var i=0, len=idArray.length; i<len; i++){
el = document.getElementById(idArray);

if (el) {
el.onclick = someFn;
}
}
}

window.onload = initOnclicks;


Or call the initClicks() function just after the last of the elements
that need an onclick has loaded.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top