Answer the question
In order to leave comments, you need to log in
How to pass .val() on click to input?
Good evening.
ran into a small problem.
I have several blocks.
<div class="block-info">
<span class="block-food">Сосиска в тесте</span>
<button class="block-btn">Заказать</button>
</div>
<div class="block-info">
<span class="block-food">Сосиска без теста</span>
<button class="block-btn">Заказать</button>
</div>
//один инпут
<input class="out" type="text">
<button class="block-btn">Заказать</button>
<input class="out" type="text">
$('.block-info').on('click', '.block-btn' function(){
var info = $(this).find('.block-food').val();
$('.out').val(info).html('заказать')
})
Answer the question
In order to leave comments, you need to log in
$('.block-btn').on('click', function(){
var food = $(this).parent().find('.block-food').text();
// или так
// var food = $(this).prev().text();
$('.out').val(food);
// или со словом "заказать"
// $('.out').val(food + ' (заказать)');
});
$('.block-btn').click(function(){
var info = $(this).closest('.block-info').find('.block-food').text();
$('.out').val(info);
})
You need to use the .parent() property:
$(document).ready(function() {
$('.block-btn').on('click', function() {
var text = $(this).parent().find('.block-food').text();
$('.out').val(text);
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question