How to assign event handler in css?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    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 );
    }
  • RobG

    #2
    Re: How to assign event handler in css?

    On Nov 14, 8:04 am, Chris <spam_me_...@go away.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.f oo’, 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 getElementsByCl assName 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
    getElementsByTa gName) whereas native javascript methods tend to return
    a javascript Array.


    --
    Rob

    Comment

    • dhtml

      #3
      Re: How to assign event handler in css?

      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 documentClickHa ndler(ev) {
      ev = ev || window.event;
      var target = ev.target || event.srcElemen t;

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

      target.innerHTM L = "you clicked me!";

      }

      document.onclic k = documentClickHa ndler;


      --
      comp.lang.javas cript FAQ <URL: http://jibbering.com/faq/ >

      Comment

      • dhtml

        #4
        Re: How to assign event handler in css?

        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 documentClickHa ndler(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.t agName) return;

        target.innerHTM L = "you clicked me!";

        }

        document.onclic k = documentClickHa ndler;


        --
        comp.lang.javas cript FAQ <URL: http://jibbering.com/faq/ >

        Comment

        Working...