Reputation: 1909
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.
Upvotes: 0
Views: 88
Reputation: 1936
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"));
})
Upvotes: 0
Reputation: 4812
.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');
});
data-avl
as an attribute to make it valid. .sibling()
function to select any of the siblingsinput
element is always going to be the next element, use the .next()
functionUpvotes: 0
Reputation: 15555
$(".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">
.next()
since input is next to the click elementUpvotes: 3