0

I'm in a situation as follows:

<span class="inputgrp">
  <span class="additem">

  </span>
</span>
<input type="text" avl="abc">

I'm trying to get the value of the attribute avl from the input with a click event on class="inputgrp"

I have tried with:

$(".additem").click(function () {
  var v = $(this).parent().find("input:text").attr("avl")
})

and

$(".inputgrp").click(function () {
  var v = $(this).prev("span").next("input:text").attr("avl")
})

But without success. Would appreciate some guide as I have no clue what I am dong wrong.

3 Answers 3

3

$(".inputgrp").click(function() {

  alert($(this).next("input:text").attr("data-avl"))

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="inputgrp">
  <span class="additem">
1
  </span>
</span>
<input type="text" data-avl="abc">

  1. Use data attribute since avl is not a valid attribute
  2. use .next() since input is next to the click element
Sign up to request clarification or add additional context in comments.

Comments

0

.find searches for children of the current element. The input is not a child but a sibling of .inputgrp

$('.inputgrp').on('click',function() {
    var v = $(this).siblings('input[data-avl]').attr('data-avl');
});
  • Use data-avl as an attribute to make it valid.
  • Use the .sibling() function to select any of the siblings
  • If the input element is always going to be the next element, use the .next() function

Comments

0

use a data-avl="" attribute on your html input element and in jQuery use .data('avl') to get the value of the attribute

$(".inputgrp").click(function() {

  alert($(this).next("input:text").data("avl"));

})

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.