views:

465

answers:

1

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..

+1  A: 

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.

Peter Bailey
peter: why, it will work irregardless of the disabled - perhaps it won't be changing it visually on-screen but in creating a custom click event for the element, you can interact with it nonetheless. things I'd change is this.value -> this.get("value") (best practice and allowing you to use string prototypes) and drop the selector probably can stay as getElements("input") unless they start to vary.
Dimitar Christoff
@Dimitar - I'm confused - are you saying that DOM events fire from a disabled form element? Because that's not true. I encourage you to do a simple test to see for yourself `<input type="text" disabled="" onclick="alert('test');" />`. Sure, you can register an event listener, but it will never fire since those events will never be dispatched into the DOM.
Peter Bailey
hrm - you are correct, i did not know that.
Dimitar Christoff