user3724046
user3724046

Reputation: 1

How to differenciate 2 buttons calling the same jquery function

I have 2 textfields and 2 buttons calling the same function.

<input type="text" id="value1"/>
<input type="button" value="Get value" id="getvalue"/><br>

<input type="text" id="value2"/>
<input type="button" value="Get value" id="getvalue"/><br>

$('#getvalue').click(function(){       
        recording one value at a time
});

How can I record each individual values from the same function. Any advices would very welcomed.

Thanks.

Upvotes: 0

Views: 66

Answers (3)

ashish
ashish

Reputation: 245

You can do it also as per below:

<input type="text" id="value1"/>
<input type="button" value="Get value" data="val1" id="getvalue"/><br>

<input type="text" id="value2"/>
<input type="button" value="Get value" data="val2" id="getvalue"/><br>

$('#getvalue').click(function(){       
       var data=$(this).attr('data');
if(data=="val1")
{
alert($('#value1').val());
}
else if(data=="val2")
{
alert($('#value2').val());
}
});

Upvotes: 0

Suhas Gosavi
Suhas Gosavi

Reputation: 2180

Try this- Working Fiddle

And i agree with Mr.Alien

$('#getvalue').click(function(){       
   var value1 = $('#value1').val();
   var value2 = $('#value2').val();
   alert(value1);
   alert(value2);
});

Upvotes: 0

Gabe
Gabe

Reputation: 462

Id's should be unique.

Why don't u use the code like this.

<input type="text" id="value1"/>
<input type="button" value="Get value" class="getvalue" id="btn1" /><br>

<input type="text" id="value2"/>
<input type="button" value="Get value" class="getvalue" id="btn2"/><br>

$('input.getvalue').click(function(){       
   if (this.id == "btn1") {
   }
   if (this.id == "btn2") {
   }
});

Upvotes: 4

Related Questions