Here's my HTML code,
$(".slide").each(function() { var a = $(this).find(".input-field"), b = $(this).find("a"); b.click(function() { console.log(a.value()); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" class="step-form d-flex"> <div class="slide"> <h3>Enter your code1</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> <div class="slide"> <h3>Enter your code2</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> </form>
I'd like to display the entered text on the input when the button was click.
I've been try this code. but I always results to undefined;
In jQuery you need to use val()
instead of value()
( API ):
$(".slide").each(function() { var a = $(this).find(".input-field"), b = $(this).find("a"); b.click(function() { console.log(a.val()); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" class="step-form d-flex"> <div class="slide"> <h3>Enter your code1</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> <div class="slide"> <h3>Enter your code2</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> </form>
You can try :
$(".btn-start").click(function() { console.log($(this).closest('.slide').find('.input-field').val()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="#" class="step-form d-flex"> <div class="slide"> <h3>Enter your code1</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> <div class="slide"> <h3>Enter your code2</h3> <input type="text" class="input-field" placeholder="ZIP CODE"> <p><a href="#" class="btn-start">Start now!</a></p> </div> </form>
It should be .val() not .value()
$(".slide").each(function() {
var a = $(this).find(".input-field"),
b = $(this).find("a");
b.click(function() {
console.log(a.val());
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.