Hi I try to receive a value from span from table like this:
function getValueFromSibling(this) {
var id = $(this).parent().siblings('span.childSibbling');
}
Table looks like this:
<tr>
<td>
<button type="button" onClick="getValueFromSibling()"></button>
</td>
<td>
<span class="childSibbling">100</span>
</td>
</tr>
But I receive something like this:
id = r.fn.init [prevObject: r.fn.init(1)]
I found that is simple form of:
var jQuery = function( selector, context ){
return new jQuery.fn.init( selector, context );
};
So there is the question. How to receive InnerHTML
from <span>
, or how to convert r.fn.init [prevObject: r.fn.init(1)]
to value?
var result = id.val();
and var result = id.get();
dosen't work
fn. init( selector, context ); }; So when you use a selector you are creating an instance of the jquery function; when found an element based on the selector criteria it returns the matched elements; when the criteria does not match anything it returns the prototype object of the function.
fn is an alias for jQuery. prototype which allows you to extend jQuery with your own functions. For Example: $.fn. something = function{}
version added: 1.0jQuery( "element" ) Refers to the tagName of DOM nodes.
First of all, this
inside your function getValueFromSibling
is in window scope pass this
context when calling the function
onClick="getValueFromSibling(this)"
And there are few things missing like text()
to get the text you want. Try one below
Better way (actually recommended way):
Html
<tr>
<td>
<button type="button" class="some-button"></button>
</td>
<td>
<span class="sibbling">100</span>
</td>
</tr>
Jquery
$(function(){
$('.some-button').click(function(){
var id = $(this).closest('tr').find('.sibbling').text();
// .closest() gets the closest tr parent .
// .find() finds the element with class 'sibbling' inside that `tr`
// .text() gets the text inside the element.
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With