Will
Will

Reputation: 69

Fill a hidden input with value Javascript

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..

Upvotes: 2

Views: 808

Answers (5)

Bhargav Chudasama
Bhargav Chudasama

Reputation: 7165

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">

Upvotes: 0

Jay Zamsol
Jay Zamsol

Reputation: 1283

var row_col_nr = $('[name^="nr"]').val();

Hope this will help

Upvotes: 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">

Upvotes: 1

Sunil Dora
Sunil Dora

Reputation: 1472

$('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.

Upvotes: 0

Abhishek Anand
Abhishek Anand

Reputation: 477

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]"]')

Upvotes: 0

Related Questions