2

I formed a Json String.

var jsonProduct = "{Product:'" + Details[0] + "',Brand:'" + Details[1] + "',Model:'" + Details[2] + "',Price:'" + Details[3] + "'}"

<input class="button black" type="submit" value="Add To Cart" onclick="return addOrderItem(' + jsonProduct + ')" />

How to pass this 'jsonproduct to javascript function addOrderItem as follows

function addOrderItem(product)
{
    cartproduct[cartproduct.length] =  " + product + ";
    //cartproduct[cartproduct.length] = " + {Product:'1001',Brand:Dell',Model:'Inspiron',Price:'25000'} + ";
}

When I pass product as parameter it is not working

1
  • Why are you handling the click on a submit input ? Don't you want to handle the submit event on the form instead ? Commented May 31, 2013 at 11:07

1 Answer 1

4

You could parse it using

var product = JSON.parse(jsonProduct);

but you don't have to use JSON at all. Do this :

var product = {
    Product: Details[0], Brand:Details[1],
    Model:Details[2], Price:Details[3]
};
addOrderItem(product);

If you want to call this from an input click, you can bind the call using

onclick="return addOrderItem(product)"

or, better, give an id to your element and then bind the event handler from the JS code :

<input id=submit class="button black" type="submit" value="Add To Cart">
<script>
    document.getElementById('submit').onclick=function(){addOrderItem(product)};
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.