/    Sign up×
Community /Pin to ProfileBookmark

Generic event listener

Greetings,

I’m developing a client-side application that has 200+ input boxes. Every one of these input boxes has ONCLICK=”this.select()” attached to it. How would I make a generic event listener for onclick for all input boxes on a page?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Orc_ScorcherOct 17.2006 — document.onclick = function clickHandler(ev) {
with ((ev && ev.target) || event.srcElement)
if (nodeName == "INPUT" && type == "text")
select()
}
Copy linkTweet thisAlerts:
@KravvitzOct 17.2006 — Don't use with(). [url=http://www.dynamicsitesolutions.com/javascript/badly_written_scripts/]Some signs that a JavaScript Script was Badly Written[/url]

TNO, I suggest you read these:

[url=http://www.digital-web.com/articles/the_behavior_layer/]The Behavior Layer[/url]

[url=http://www.digital-web.com/articles/separating_behavior_and_structure_2/]Separating behavior and structure[/url]

[url=http://www.alistapart.com/articles/behavioralseparation/]Behavioral Separation[/url]

[url=http://www.onlinetools.org/articles/unobtrusivejavascript/]Unobtrusive JavaScript[/url]

[url=http://www.domscripting.com/book/sample/]DOM Scripting - Sample chapter: Best Practices[/url]

[url=http://www.severnsolutions.co.uk/articles/001/]Accessible DHTML[/url]
Copy linkTweet thisAlerts:
@ProggerPeteOct 18.2006 — What kravvitz said. Better might be,

function clickHandler(e)
{
var src = e ? e.target : event.srcElement;
if (src.nodeName == 'INPUT' && src.type =="text") select();
}
document.onclick = clickHandler;
Copy linkTweet thisAlerts:
@mrhooOct 18.2006 — Or, You can add this bit to an onload event,

or define it as a function and call it onload:

[CODE]function clickSelect(){
var A= document.getElementsByTagName('input');
var L= A.length, tem, fun;
for(var i= 0; i< L; i++){
tem=A[i];
fun= function(){tem.select()};

if(tem.addEventListener) tem.addEventListener('click',fun,false);
else if(tem.attachEvent) tem.attachEvent('onclick',fun);

else tem.onclick= fun;
}
}[/CODE]
×

Success!

Help @TNO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...