jasonc Posted August 18, 2010 Share Posted August 18, 2010 I have tried the following but it is not working. what is the correct way to do this? document.getElementById('MyHiddenField').value = "1"; Link to comment https://forums.phpfreaks.com/topic/211069-how-to-change-value-of-hidden-field-using-javascript/ Share on other sites More sharing options...
AtlasC1 Posted August 18, 2010 Share Posted August 18, 2010 What does the HTML of the form look like? -jm Link to comment https://forums.phpfreaks.com/topic/211069-how-to-change-value-of-hidden-field-using-javascript/#findComment-1100767 Share on other sites More sharing options...
RIRedinPA Posted August 18, 2010 Share Posted August 18, 2010 This worked for me: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en"> <head> <meta charset="utf-8"> <title>Page Title</title> <!-- meta tags --> <meta name="keywords" content=""> <meta name="description" content=""> <!-- javascript --> <script language="javascript"> function changeHidden() { var theform = document.myform; var hiddenvalue = theform.hiddeninput.value; if (hiddenvalue == "testme") { theform.hiddeninput.value = "loremipsum"; } else { theform.hiddeninput.value = "testme"; } alert(theform.hiddeninput.value); } </script> <!-- stylesheets --> <!--conditional comments --> </head> <body> <form id="myform" name="myform"> Hidden input... <input type="hidden" value="testme" id="hiddeninput" name="hiddeninput"> </form> <a href="javascript:void(0);" onmousedown="changeHidden();">Click Me</a> </body> </html> Link to comment https://forums.phpfreaks.com/topic/211069-how-to-change-value-of-hidden-field-using-javascript/#findComment-1100830 Share on other sites More sharing options...
radar Posted August 18, 2010 Share Posted August 18, 2010 document.formname.getElementById('hiddeninput').value = 'boo!'; should do the trick. much like the code above me states, except in a shorter fashion. also keep in mind that users who are using ie8 will get a less than perfect response from your code... reasining behind this it seems as though IE8 doesn't negate a href command when it runs into an onclick command.. and so you may want to do something like: href="index.php" onclick="changeHidden(); return false" that way it forces ie8 to not use the href. Link to comment https://forums.phpfreaks.com/topic/211069-how-to-change-value-of-hidden-field-using-javascript/#findComment-1100929 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.