How to capture the value of the current attribute with jQuery?

1

I have the following form in html:

<form>First name:
    <br>
    <input type="text" name="nome">
    <br>Last name:
    <br>
    <input type="text" name="sobrenome">
</form>

And the jQuery code:     $ (document) .ready (function () {

    $('input').change(function()
    {   
        alert($(this).attr('name'));
    });
});

By changing the value of a given field I want to give an alert with the name of the attribute, but I wanted to do this without having to enter the tag of the selector, ie instead of informing the input I want it to be retrieved automatically based on the element I'm currently changing. How do I do this?

    
asked by anonymous 17.11.2015 / 15:00

1 answer

4

* for all elements.

$('*')

If you want to make a list of elements separate by , :

$('input,select,textarea')

You can also use filter :

jQuery('*').filter(function(){
    var accept = [
        'input',
        'select',
        'textarea'
    ];
    var tag = jQuery(this).prop('nodeName');
    return jQuery.inArray(tag.toLowerCase(), accept);
});
    
17.11.2015 / 15:08