Vince V.
Vince V.

Reputation: 3143

Mootools: Get the clicked element value

Hey guys I'm currenlty working on something and I want if I click a textbox element the value of it. the only problem is that it doesn't have an ID.

my html code:

<ul id="textbox">
    <li><input type="text" value="test1" name="chosen" disabled="disabled" /></li>
    <li><input type="text" value="test2" name="chosen" disabled="disabled" /></li>
    <li><input type="text" value="test3" name="chosen" disabled="disabled" /></li>
    <li><input type="text" value="test4" name="chosen" disabled="disabled" /></li>
</ul>

my mootools code:

window.addEvent('domready', function() {

    var el = $('textbox');

});

If I click the first textbox I want to alert test1. If I click the second one I want to alert test2

I was thinking of adding ID's to the textboxes, but the textboxes are variable...

Thanks in advance..

Upvotes: 1

Views: 2616

Answers (1)

Peter Bailey
Peter Bailey

Reputation: 105908

First of all, you have a problem. Disabled form elements don't fire DOM events, so you'll have to figure out how you want to handle that.

Secondly, to your question. MooTools has a selector to do what you want. I believe the below snippet will work (i'm no mootools expert)

var el = $('textbox');
el.getElements('input[name=chosen]').addEvents({
    'click': function()
    {
        alert( this.value );
    }
});

Again, this won't work unless you remove the disabled="disabled" from your inputs.

Upvotes: 2

Related Questions