printing a value in a hidden form field?

Discussion in 'JavaScript' started by mokimofiki, Jan 22, 2011.

  1. #1
    This is the line within the html that I need to have a javascript variable added to:
    <input type='hidden' name='multi' id='multi' value='179,1;197,1;430,1;316,1'>
    Code (markup):
    Currently this is what I have which I know is wrong:
    <input type='hidden' name='multi' id='multi' value='<script type=Javascript>document.write(s1);</script>,1;179,1;197,1;430,1;316,1'>
    Code (markup):
    s1 is defined earlier on the page with a 3 digit number that needs to be passed with the others in the hidden field ... without the <script type=Javascript>document.write(s1);</script> everything processes fine any thoughts?
     
    mokimofiki, Jan 22, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this out:

    form.html
    
    <html>
    <head>
    <script type="text/javascript">
    	var s1 = '123';
    	function changeValue() {
    		var s2 = document.getElementById('multi').getAttribute('value');
    		s2 = s1 + ',1;' + s2;
    		document.getElementById('multi').setAttribute('value',s2);
    	}
    </script>
    </head>
    <body>
    <form method='GET' onsubmit='changeValue();' action='result.html' >
    <input type='hidden' name='multi' id='multi' value='179,1;197,1;430,1;316,1' />
    <input type='submit' value='submit' />
    </form>
    </body>
    </html>
    
    Code (markup):
    result.html
    
    <html>
    <head>
    <script type="text/javascript">
    	var s = location.search;
    	var i = s.indexOf('multi=');
    	s = s.slice(i+6, s.length);
    	i = s.indexOf('&');
    	s = s.slice(0, i);
    	document.write(unescape(s));
    </script>
    </head>
    <body>
    </body>
    </html>
    
    Code (markup):
    Hope that works :)
     
    Cash Nebula, Jan 22, 2011 IP