2

I am new on js and I am struggling with this task can someone please give me a little help.. This is my code:

<td><input type="radio" name="nr[1]" value="1" class="col"></td>
<td><input type="radio" name="nr[2]" value="2" class="col"></td>
<td><input type="radio" name="nr[3]" value="3" class="col"></td> 
<td><input type="radio" name="nr[4]" value="4" class="col"></td>

Here I got the hidden input that I need to fill with values that come from the js onchange event..

 <input type="hidden" name="row_col_nr[5]" value="0" class="row_nr"> 

And here I have my js code:

var row_col_nr = null;
$(".col").on("change", function () {
   row_col_nr = $( this ).val();
   alert(row_col_nr); // gives me the value of input that I select

});

My problem is that I do not know how take that row_col_nr values and put to the hidden input I do not know if I am telling right what is my problem.. If someone understands me can you please give me a little help..

3
  • 1
    what element is minimal Commented Nov 8, 2017 at 9:16
  • where did you apply minimal class? Commented Nov 8, 2017 at 9:16
  • sorry I eddited my code Commented Nov 8, 2017 at 9:22

5 Answers 5

1

To set the value of the hidden input use the following code:

$(".row_nr").val(row_col_nr)

Working demo below

var row_col_nr = null;
$(".col").on("change", function() {
  row_col_nr = $(this).val();
  alert(row_col_nr); // gives me the value of input that I select
  $(".row_nr").val(row_col_nr)
  console.log("the value of the hidden input is: " + $(".row_nr").val())
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><input type="radio" name="nr[1]" value="1" class="col"></td>
<td><input type="radio" name="nr[2]" value="2" class="col"></td>
<td><input type="radio" name="nr[3]" value="3" class="col"></td>
<td><input type="radio" name="nr[4]" value="4" class="col"></td>

<input type="hidden" name="row_col_nr[5]" value="0" class="row_nr">

Sign up to request clarification or add additional context in comments.

1 Comment

This works fine but when I submmit the the row_col_nr gives me an array of only the last numbers like this 1=>4, 2=>4, 3=>4 .. maybe this is because the inputs I have inside a for and the input looks like this: @for($r = 1; $<=4; $r++) <input type="radio" name="col_nr[{{ $r }}]" value="1" class="col"> <input type="radio" name="col_nr[{{ $r }}]" value="2" class="col"> ... do I need to use .each or something so that value to come [1,2,3,4] not only the last selected.. hope you understand me thanks again .
1
var row_col_nr = $('[name^="nr"]').val();

Hope this will help

Comments

0

Give an id, say hidden_input to the input which is hidden then use the following code:

$(#hidden_input).val(row_col_nr );

If you don't want to add the id you can use the following:

$('input[value=row_col_nr][name="row_col_nr[5]"]')

Comments

0

$('form').on('submit', function(e){
    $('[name="retype-email"]').val($('[name="email"]').val());
    
    e.preventDefault()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>Email:
    <input type="text" name="email">
    <br>
        <label>Hidden input: (visible for example purposes)</label><input name="retype-email">
    <br>
    <input type="submit" value="Submit">
</form>

Here is an example or you may visit URL

Hope this will helps you.

Comments

0

use $('.row_nr').val(row_col_nr); for store into hidden input

var row_col_nr = null;
$(".col").on("change", function() {
  row_col_nr = $(this).val();
  alert(row_col_nr); // gives me the value of input that I select
  $('.row_nr').val(row_col_nr);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><input type="radio" name="nr[1]" value="1" class="col"></td>
<td><input type="radio" name="nr[2]" value="2" class="col"></td>
<td><input type="radio" name="nr[3]" value="3" class="col"></td>
<td><input type="radio" name="nr[4]" value="4" class="col"></td>
Here I got the hidden input that I need to fill with values that come from the js onchange event..

<input type="hidden" name="row_col_nr[5]" value="0" class="row_nr">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.