1

I have a div block where I am trying to find the name of the input element which was clicked and then update the value using jQuery:

$(document).ready(function(){
  $(this).click(function(){
    console.log($(this).attr('name'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <input type="text" name="name1" id="id1">TestData1</input>
<input type="text" name="name2" id="id2">TestData2</input>
</div>

When I try to find the input element which was clicked I get undefined.

I am assuming it may be because I am checking click event on this and then again trying to get the attr name of it, but since it can increase to at least 5 input elements and these are being fetched dynamically, I cannot bind the id/class with click event.

4
  • 1
    Just a heads up.. <input tags are self-closing.. like so <input type="text" name="name1" id="id1" /> Commented Feb 9, 2017 at 17:37
  • Use event delegation since you say you get elements dynamically.. $(document).on('click','input', function... Commented Feb 9, 2017 at 17:38
  • The value of this is not what you think it is. Commented Feb 9, 2017 at 17:39
  • JSFiddle, then right-click and hit Inspect.. go to console.. and click in the textboxes Commented Feb 9, 2017 at 17:40

3 Answers 3

3

The this has no context inside ready function :

$(this).click(function(){
_^^^^^___________________
   console.log($(this).attr('name'));
});

You should use input :

$('input').click(function(){
   console.log($(this).attr('name'));
});

NOTE ALSO : the input tag is self closed :

<input type="text" name="name1" id="id1" value='TestData1'/>
<input type="text" name="name2" id="id2" value='TestData2'/>

I hope this will helps.

$(document).ready(function(){
  $('input').click(function(){
    console.log($(this).attr('name'));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div>
  <input type="text" name="name1" id="id1" value='TestData1'/>
<input type="text" name="name2" id="id2" value='TestData2'/>
</div>

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

Comments

2

Why not bind by element instead?

$("input").click(function(){
   console.log($(this).attr('name'));
});

Comments

0

You are binding the click to document(Simply entire web page) and document doesn't have attribute called name. It will return undefined on console.

As other answers suggests, I'll also suggest binding click event to input instead of document.

If you want to bind click with document and find the target clicked element by using event which will be automatically available as an argument on click handler. Try with following snippet

$(document).ready(function(){
  $(this).click(function(event){
    console.log($(event.target).attr('name'));
  });
});
<div>
  <input type="text" name="name1" id="id1">TestData1</input>
  <input type="text" name="name2" id="id2">TestData2</input>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Here you access event target element on which you clicked and using it to get name attribute value. It returns name attribute value if your target element has name attribute else undefined will be returned.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.