Search code examples
selectmootoolsblur

mootools select box focus


I have a select box

 <select>
     <option value="0">0 mins</option>
     <option value="1">1 mins</option>
     <option value="2">2 mins</option>
 </select>

and I want to fire an event when the visitor either clicks on a value or clicks anywhere else on the page -i.e loss of focus on the select box

I've fiddled about for four hours now with no joy. I'm now down to this:

var c = 0;
$("selectTime").addEvent('click', function() {
    if (c++ % 2 == 1) {
        console.log(c);
       //$(this).blur();
    }
});

$('selectTime').click(function() {
    if ($('select').is(':blur')) {
        c = 1;
    } else {
        c = 0;
    }
});

any ideas?

thanks


Solution

  • The mootools syntax for adding multiple events to same element is:

    $('myElementID').addEvents({
        blur: function(){
            alert('blur');
        },
        click: function(){
            alert('click');
        }
    });
    

    Example with you code

    You could though use just the change event, which fires when the element is changed. Like:

    $('myElement').addEvent('change', function(){
        alert('Select changed');
    });
    

    Example

    Note that part of your code is using jQuery syntax, part is using MooTools syntax.