views:

29

answers:

2

I want to simulate clicking with the mouse on all elements of a given class ("x-grid3-col-value"). I didn't manage to do this with ExtJS as the function "fireEvent" only applies to ExtJS components. How could I do?

+2  A: 

You will get all the items with that class this way:

var items = Ext.query('.x-grid3-col-value');

It will give you an array of all the items. Then you can go by each item and place a handler on each item:

    Ext.each(items, function (item) {
        item = Ext.get(item);

        item.on('click', function () {  
            //'click' or 'dblclick' or 'mouseover'
            //console.log(item);
            //Do whatever you want
        }, this);
    }, this);
Swar
A: 

I have used this snippet, which is quite ugly, but works.

function getElementsByClassName(classname, node) {
    if (!node) {
        node = document.getElementsByTagName('body')[0];
    }
    var a = [], re = new RegExp('\\b' + classname + '\\b');
    els = node.getElementsByTagName('*');
    for ( var i = 0, j = els.length; i < j; i++) {
        if (re.test(els[i].className)) {
            a.push(els[i]);
        }
    }
    return a;
}

HTMLElement.prototype.click = function() {
    var evt = this.ownerDocument.createEvent('MouseEvents');
    evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1,
            0, 0, 0, 0, false, false, false, false, 0, null);
    this.dispatchEvent(evt);
}

var elts = getElementsByClassName("x-grid3-col-value", window.innerHTML);
var i = 0;

while (i < elts.length) {
    var elt = elts[i];
    elt.click();
    i++;
}
Pierre Gardin