12

Creating a plugin in jQuery, I have to get the names of input fields on which events would occur.

Suppose:

<input type="text" name="name" />
<input type="text" name="age" />

Whether submitted or not doesn't matter. What I want is to get the name of the field.

I tried the following:

$('#ins').html($(this).attr('name').val());

#ins is the div, where I am writing the events such as a click was made on the input with name = name. This way I want to create a plugin which would guide me to get to know which fields are getting some events.

3
  • 2
    $(this).attr('name') is enough Commented Oct 10, 2013 at 19:47
  • Could you post the whole of your plugin? Without context it's hard to offer a valid suggestion. Commented Oct 10, 2013 at 19:54
  • @Peter has provided the perfect and in time answer :) It was the val() thing messing up! Now its perfect :) Commented Oct 10, 2013 at 19:57

2 Answers 2

17

You are bit right but when you trying to access attribute you have to use only the attr not the val().

$(this).attr('name')

The above code is enough to get the name of the event triggered elements name attribute.

Sign up to request clarification or add additional context in comments.

Comments

1

try this:

$("input[type=text]").each(function(){
   $('#ins').html($(this).attr('name') + " <br>");
});

with this code, you will get the name attribute of all input in the page. And i put a "br" just to break a line.

2 Comments

its fair now, and just as i wanted! :D I wanted the <br /> too :)
its difficult in case of dimensional field name

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.