473,382 Members | 1,665 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,382 software developers and data experts.

How to assign event handler in css?

I'm guessing the answer is no, but is it possible to assign an event
handler to a control using css?

For example, I'd like to assign an onclick handler to every <divthat
has a class of "foo". Possible?

div.foo {
onclick: myFunction(this);
}
Nov 13 '08 #1
3 4818
On Nov 14, 8:04 am, Chris <spam_me_...@goaway.comwrote:
I'm guessing the answer is no, but is it possible to assign an event
handler to a control using css?
For some value of “possible”, yes, you can.

For example, I'd like to assign an onclick handler to every <divthat
has a class of "foo". Possible?

div.foo {
onclick: myFunction(this);

}
There are various libraries that allow selection of elements using a
CSS-style selector, returning an array of the selected elements. Some
use a mixture of XPath and native functions, others just native. The
call would be something like:

var nodeArray = selector(‘div.foo’, rootNode);
XPath is not widely implemented, but for those UAs that have it, it’s
very fast (host-level functions tend to be much faster than equivalent
native javascript functions).

Have a look at how those libraries do it, or you could write your own
simple function that implements just the selectors you need - things
like getElementsByClassName are supported by some browsers and pretty
simple to implement otherwise.

Where host support is provided, a NodeList (a live list of the
selected elements) is generally returned (mimicking the behaviour of
getElementsByTagName) whereas native javascript methods tend to return
a javascript Array.
--
Rob
Nov 13 '08 #2
Chris wrote:
I'm guessing the answer is no, but is it possible to assign an event
handler to a control using css?

For example, I'd like to assign an onclick handler to every <divthat
has a class of "foo". Possible?

div.foo {
onclick: myFunction(this);
}
Event bubbling instead is usually more efficient. Example:-
function documentClickHandler(ev) {
ev = ev || window.event;
var target = ev.target || event.srcElement;

// If the user did not click a paragraph, exit.
if(!/p/i.test(target.tagName) return;

target.innerHTML = "you clicked me!";

}

document.onclick = documentClickHandler;
--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
Nov 15 '08 #3
Chris wrote:
I'm guessing the answer is no, but is it possible to assign an event
handler to a control using css?

For example, I'd like to assign an onclick handler to every <divthat
has a class of "foo". Possible?

div.foo {
onclick: myFunction(this);
}
Event bubbling instead is usually more efficient. Example:-
function documentClickHandler(ev) {
ev = ev || window.event;
var target = ev.target || ev.srcElement;

// If the user did not click a paragraph, exit.
if(!/p/i.test(target.tagName) return;

target.innerHTML = "you clicked me!";

}

document.onclick = documentClickHandler;
--
comp.lang.javascript FAQ <URL: http://jibbering.com/faq/ >
Nov 15 '08 #4

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

Similar topics

3
by: N. Demos | last post by:
How do you dynamically assign a function to an element's event with specific parameters? I know that the code is different for MSIE and Mozilla, and need to know how to do this for both. I...
4
by: John E Katich | last post by:
How do I assign a unique Id (like WM_USER+nnn) to a control?
3
by: Pavils Jurjans | last post by:
Hello, I am looking for solution to assign the Session.onEnd event handler dynamically, at runtime, without using global.asax file. I am a bit sceptic wether that is possible, however I thought...
7
by: Dave R | last post by:
Anyone know how to wire up an event handler for controls added to an ASP.NET page at run-time? Of course, at design-time this is easy: just enter the method's name in the event tab of the...
11
by: webgour | last post by:
Hello, I am wondering why the following works, on IE6, but with an error : "Not implemented". function TEST(){} TEST.prototype.Initialize = function() { var mImage = new Image(); var mDate...
18
by: joaotsetsemoita | last post by:
Hello everyone, I'm having troubles assigning an onclick event to a cell. Im trying something like cursorPoint.cells.style.cursor = "hand"; cursorPoint.cells.width = "20";...
2
by: Carla Simeoni | last post by:
Assume I have an open Form. Then I click the "normal" window "X" icon (=Close button). How do I assign a function in Form.cs to this event ? In other words if this Form/window is closed I want to...
3
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, I've got a recursive data model, where the root object gets a certain event handler: DataObject root = new DataObject(); root.DataObjectCreated += new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.