Solved

javascript value to html hidden input value

Posted on 2014-12-31
2
255 Views
Last Modified: 2014-12-31
On an e-commerce product page I have the add to basket button set to call a javascript function. The function creates 2 objects and for testing they are displayed in an alert box just before the last line of javascript fires the add to basket submit process.

alert(obj.image_id + ' AND ' + obj.image_name);
 
I've added 2 hidden input fields and I'm trying to set the value of these fields equal to the 2 javascript objects. Without any luck.

<html>
 <head> 
 <script>
  var req;
  function screenshot() {
	  $('#btncapture').val('Adding to cart...');
	  $('#btncapture').attr('disabled', 'disabled');
	  if (req) { req.abort(); } 
	  req = $.ajax({
		type: 'POST',
		url: 'screenshot.php',
		data: "containt="+$('#main-container').html().replace(/&/g, "%26"),
		cache: false,
		success: function(response){
			var obj = $.parseJSON(response);
			alert(obj.image_id + ' AND ' + obj.image_name);
			document.getElementById('newimage_id').innerHTML = obj.image_id;
			document.getElementById('newimage_name').innerHTML = obj.image_name;
			$('#PRODProducts').submit();
		}
	});
  }
</script>
</head>
<body>      
        <form NAME="PRODProducts" id="PRODProducts" method="post" action="Screen=BASK">
          <input type="hidden" name="Action" value="ADPR" />
          <input type="hidden" name="Product_Code" value="101" />
          <input type="hidden" name="newimage_id" id="newimage_id" value="overwrite this ID value" />
          <input type="hidden" name="newimage_name" id="newimage_name" value="overwrite this Name value" />
          <input type="hidden" name="containt" id="containt" value="" />
          <input id="btncapture" type="button" value="Add To Basket" onClick="screenshot()" class="wl-buttons" />
        </form>
</body>
</html>

Open in new window


Anyone have an idea of how to fix this?
Untitled.html
0
Comment
Question by:Gary Samuels
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40526072
use :
			document.getElementById('newimage_id').value = obj.image_id;
			document.getElementById('newimage_name').value = obj.image_name;

Open in new window

or because you've jQuery :
			$('#newimage_id').val( obj.image_id );
			$('#newimage_name').val( obj.image_name );

Open in new window

0
 

Author Closing Comment

by:Gary Samuels
ID: 40526081
works great, thanks
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 42
Jquery 7 33
How to get text of href without any ID using Javascript 9 24
Can anyone help me find this preloader? 51 43
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
This article discusses how to implement server side field validation and display customized error messages to the client.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question