Reputation: 435
I have 4 textfields with class cup. I want the different fields to get different value every time I will click the number button.
Below is my code:
$(".cup").focus(function(){
var xfield = $(this);
keypad(xfield);
})
function keypad(xfield)
{
$(".numbtn").click(function(){
var number = $(this).text();
xfield.val(number);
})
}
Upvotes: 2
Views: 62
Reputation: 2834
$(document).ready(function () {
$(".numbtn").click(function () {
$('.cup').each(function(){
var number = Math.floor((Math.random() * 9) + 1);
$(this).val(number);
})
})
});
Upvotes: 0
Reputation: 2834
$(document).ready(function () {
var xfield;
$(".cup").focus(function () {
xfield = $(this);
})
$(".numbtn").click(function () {
if ($(xfield).length == 0)
{
xfield = $(".cup:first");
}
$(xfield).val($(this).val());
})
});
Upvotes: 0
Reputation: 28513
Try this: you can store clicked cup
fields in a variable and when you click on numbtn
then set its value, see below code
$(function(){
var $clickedCup = $(".cup:first"); // variable to store clicked cup, default is first cup
$(".cup").focus(function(){
$clickedCup = $(this);
});
$(".numbtn").click(function(){
var number = $(this).text();
$clickedCup.val(number);//set value of clicked number
});
});
Upvotes: 2