Reputation: 987
I'm trying to set the value of a input box based on the click event on a list. I'm using $("#modifyText2").val()
to set the value of the input field, and it works. But I can't get the text of the div.CompanyName
.CompanyName
is a child of .shop_data
What I've tried:
$("#shops_output").on("click", ".shop_data", function() {
$("#modifyText2").val(this.children(".CompanyName").text);
});
Upvotes: 0
Views: 144
Reputation: 4063
It is because .children()
is a jQuery function.
Therefore you'll need to use $(this)
instead of the plain ol' this
, since there is no children
function attached to the normal javascript object.
Wrapping this
in the jQuery $
function is what adds all of the extra methods provided by jQuery.
$("#shops_output").on("click", ".shop_data", function() {
$("#modifyText2").val($(this).children(".CompanyName").text());
});
Specifically: $(this).children(".CompanyName").text()
Also, this only works if only one child is matched. If there is multiple, as mentioned by others, you will need to do the following:
$($(this).children(".CompanyName")[0]).text()
// or
$(this).children(".CompanyName").eq(0).text()
Upvotes: 1
Reputation: 146
Or in the jQuery way $(this).find('.CompanyName').eq(0).html()
if the content of ".CompanyName" is just text.
Upvotes: 0