Pass a javascript variable value into input type hidden value
You could give your hidden field an id:
<input type="hidden" id="myField" value="" />
and then when you want to assign its value:
document.getElementById('myField').value = product(2, 3);
Make sure that you are performing this assignment after the DOM has been fully loaded, for example in the window.load
event.
How to pass js variable into hidden form field?
Remove this <?php echo <script>demo</script>
from hidden field and leave it blank.
Write followed code
var demo =1;
$('#demo_val').val(demo);
in your script.
You can get value by
var field_vemo = $('#demo_val').val();
How to assign a javascript variable to a hidden input?
You have to add the javascript after the Input Hidden element is rendered, ie order of the code arrangement is important since you have added the javascript to execute when it is rendered in browser . Make sure no other element has the same id value "id".
I prefer to use a function to execute on form submit as follows.
<form action="myServlet" onSubmit="setVal();">
<input type="hidden" id="id" value="" name="total">
<input type="submit" value="Go!"/>
</form>
<script>
function setVal(){
var a = 2;
document.getElementById('id').value=a;
}
</script>
How to add a JS variable into a hidden form value
Try this; the description is mentioned in the code snipp ; basically
get the inner text of the span tag and assign it to the hidden input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<p class="total-box"><span>This is the text</span></p>
<input type="hidden" id="xyz" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
var totalDonation = $(".total-box>span").text();
$("#xyz").val(totalDonation); // Sets the text from total donation to your hidden input
var hiddeninput = $("#xyz").val();
console.log(hiddeninput); // displays hidden input data
</script>
</body>
</html>
Passing a js variable value in an input type hidden and then retrieve it with php
In your script
you're return
ing before you've finished your function.
You should only use return
when you want to stop the function and return a result. Anything after return will not be read.
This is what your code should look like:
<script type="text/javascript">
var nb=0;
function count_nb_submit() {
nb++;
document.getElementById('nb').value=nb;
return nb;
}
</script>
How to set a value to a hidden type input from javascript
If you give the hidden input an id, you can then query the DOM for that element and set the value, like so:
document.getElementById('userid').value = '123';
Insert jquery variable value into input hidden field
Inserting into hidden input field based on your code
$(document).ready(function() { $('.pro_img').click(function() { var imgpro = jQuery(this).attr('src'); $('#hidden_pro_image').val(imgpro); alert($('#hidden_pro_image').val()); });});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><img class="pro_img" src="https://i0.wp.com/wptavern.com/wp-content/uploads/2016/07/stack-overflow.png?ssl=1" width="100px" />
<input id="hidden_pro_image" type="hidden" value="" />
Passing JavaScript variables to PHP as a hidden input
You did'nt set the form method so it defaults to GET
You should set
<form action="search" method="POST">
try
JS :
var yes = "<?php echo $_POST['yes']; ?>";
document.getElementById('yes').innerHTML = yes;
Related Topics
What Is the Purpose of the HTML "No-Js" Class
Change Value of Input and Submit Form in JavaScript
How to Inject a Style with an "!Important" Rule
Is Ruby Strongly or Weakly Typed
Detect If Browser Tab Has Focus
Convert Special Characters to HTML in JavaScript
Passing Arguments Forward to Another JavaScript Function
Why Does JavaScript Variable Declaration at Console Results in "Undefined" Being Printed
How to Prevent an Http Request Just for a Favicon
Clean Way to Programmatically Use CSS Transitions from Js
Why Are CSS Animations and Transitions Blocked by JavaScript
Escaping </Script> Tag Inside JavaScript
How to Determine the Number of Days in a Month with JavaScript