1

I have an html like below

HTML:

 <INPUT TYPE=CHECKBOX NAME="clcik" onClick="add('1234','blah')" />
 <input type="hidden" id="project" value="" />

JS:

    function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };
    jsonArray.push(jsonObj);
    var field = document.getElementById('project');
    field.setAttribute('value', JSON.stringify(jsonArray));
    alert(field.getAttribute('value'));
    }

I am trying to set first and retrieve again to check but nothing is happening..I can't understand where I am wrong...?

5
  • 1
    Where is the stringData variable set? Commented Aug 17, 2011 at 18:39
  • @jfriend00, I'm sure that's it. Write up an answer with var stringData = JSON.stringify(jsonArray) replaced. Commented Aug 17, 2011 at 18:41
  • Its probably the output of JSON.stringify Commented Aug 17, 2011 at 18:41
  • This is not jQuery related - i've removed the tag and updated the title. Commented Aug 17, 2011 at 18:43
  • Your question are all about one and the same problem. You won't get far if you are trying to solve the problem step by step by asking questions here (and questions about solutions you get here). I really recommend you to take a step back and read about JavaScript, DOM, HTML etc so that you have a basic understanding of it and can use it. Commented Aug 17, 2011 at 20:00

5 Answers 5

3

I guess you missed to get the stringify result into stringData variable because of which you are getting a js error before it reaches the line where you are trying to alert the value.

JSON or JSON.stringify is not provided by jQuery you have to include json2 library on the page if the browser natively do not support it.

Try this

function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };

    jsonArray.push(jsonObj);
    var stringData = JSON.stringify(jsonArray);

    var field = document.getElementById('project');

    field.setAttribute('value', stringData);
    alert(field.getAttribute('value'));
}

Code to remove element from array based on your request in the comment.

var newArray = [], productIdToRemove = "1234";
$.each(jsonArray, function(){
   if(this.product_id != productIdToRemove){
      newArray.push(this);
   }
});

//Now newArray will not have '1234'
Sign up to request clarification or add additional context in comments.

10 Comments

JSON or JSON.stringify is not provided by jQuery you have to include json2 library on the page.
@Shankar most browsers will actually include JSON natively. stackoverflow.com/questions/891299/…
I think < IE8 do not have it natively
@User - You had a js error in your fiddle, take a look at this fiddle it works fine jsfiddle.net/qWCwa/8
You were not checking for empty value for the first time. Try this fiddle I fixed it jsfiddle.net/qWCwa/16
|
1

Change

JSON.stringify(jsonArray)
// to
var stringData = JSON.stringify(jsonArray);

Other problems with the fiddle, fixed: http://jsfiddle.net/mattball/qWCwa/7/

1 Comment

Open your browser's console and you will have a world of useful information at your fingertips. Namely: Uncaught SyntaxError: Unexpected token ; because there is an extra semicolon in the setAttribute() method call.
0

In your example, you never set stringData.

Comments

0

get/set the "value" property directly on the input, not getAttribute and setAttribute methods.

Comments

0

After changing the code to assign stringData, it seems to work for me:

function add(obj1 , obj2){
    var jsonArray = [];
    var jsonObj = { product_id : obj1 , name : obj2 };
    jsonArray.push(jsonObj);
    var stringData = JSON.stringify(jsonArray)
    var field = document.getElementById('project');
    field.setAttribute('value', stringData);
    alert(field.getAttribute('value'));
    }

add(1, 2);

You can see it working here: http://jsfiddle.net/jfriend00/HNuak/.

FYI, if you just run in an environment where you can see javascript execution errors (like the Chrome or Firefox debuggers), errors like this would be easy to see in the error console.

1 Comment

Your jsFiddle settings were such that the add() function definition was not in the global scope so the checkbox code couldn't find it. I changed the jsFiddle settings and now it works here: jsfiddle.net/jfriend00/YL8eU. Once again, you just need to use a JS debugger so you can see the JS errors and you would know how to see the cause of these issues in seconds.

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.