I want to get the value of textfield(only one) inside span element using jquery but couldn’t.
<span>simple span text
<a class="remove" href="javascript:" title="Remove">x</a>
<input type="hidden" name="word[ids][]" value="this is text">
</span>
Jquery code associated to remove class of anchor tag
$(".remove").live("click", function(){
alert ($(this).parent().('input:text').val()); // return undefined
//i just coded below to check whether span itself is getting or not
alert ($(this).parent().attr('tag')); // this also return undefined
if ($(this).parent().is('span'));
alert("parent is span"); // only this works
});
If the
inputis always the next sibling of theremoveelement, then this will work:Explanation of why your code does not work:
This line
$(this).parent().('input:text').val()is syntactically and logically wrong.If you want to find an element inside another one, you have to use
find[docs]:This will still not give you the value, because you have a hidden
inputelement. If you have a look at the:text[docs] documentation, you will see that this selector thus not select those. So either just use'input'or use:hidden[docs]:'input:hidden'..attr('tag')simply does not work because an HTML element has no attribute tag. You can get the tag name from the the DOM element using thenodeNameproperty: