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

Asked

Viewed 53 times

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'));
    });
});

When changing the value of a given field I want to give an Alert with the name of the attribute, however, I wanted to do this without having to inform the tag of the selector, that is, instead of informing the input I want it to be obtained automatically based on the element I am currently changing. How do I do this?

1 answer

4

* for all elements.

$('*')

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

$('input,select,textarea')

You can also use the filter :

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.