Taking Value from an Array of Inputs

Asked

Viewed 68 times

0

Good morning

How to take the value of equal inputs when clicking on the line.

function getValor(){
	var valor = $('.produtos').val();
	 alert(valor)
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input value="1" name="valor[]" class="produtos" type="button" onclick="getValor()">
<input value="2" name="valor[]" class="produtos" type="button" onclick="getValor()">
<input value="3" name="valor[]" class="produtos" type="button" onclick="getValor()">
<input value="4" name="valor[]" class="produtos" type="button" onclick="getValor()">
<input value="5" name="valor[]" class="produtos" type="button" onclick="getValor()">

1 answer

5

You can pass the this as argument of this function call and thus access the value of this button.

Example:

function getValor(input) {
  var valor = input.value;
  alert(valor);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input value="1" type="button" onclick="getValor(this)">
<input value="2" type="button" onclick="getValor(this)">
<input value="3" type="button" onclick="getValor(this)">
<input value="4" type="button" onclick="getValor(this)">
<input value="5" type="button" onclick="getValor(this)">

Another way would be more "It’s there jQuery" thus:

function getValor() {
  var valor = this.value;
  alert(valor);
}
$('.produtos[name="valor[]"]').click(getValor);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input value="1" name="valor[]" class="produtos" type="button">
<input value="2" name="valor[]" class="produtos" type="button">
<input value="3" name="valor[]" class="produtos" type="button">
<input value="4" name="valor[]" class="produtos" type="button">
<input value="5" name="valor[]" class="produtos" type="button">

  • 1

    Thank you very much Sergio

  • @frodrigues I joined one more option

  • 1

    If the answer is correct don’t forget to mark with the right.

  • It was not cool to post not, but actually I want to get the value of an image by clicking on it. How would it be

  • Hello @frodrigues, it seems to me that you are still struggling, I think you want to use jQuery within the function, if that’s the case, use the first example of @Sergio just you pass input to the selector from jQuery, getting like this $(input) so you’ll be able to use $(input).val(), I say this because you are trying to edit this answer to put the question code, if there is still any doubt comment on the answer or edit your question.

  • @frodrigues my answer answers your question. If your problem is another you should ask another question. You can’t ask a question with the problem A and expect me to respond with solution to the problem B... if you want an image attribute you can use something like https://answall.com/q/197703/129

  • ok. I’ll do another. Thanks for the help.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.