0
<form id="target" method="POST" action="">
<input type="hidden" name="item" id="txtEmail"   value='{"_id":"55a0af70b2a45442078b196d","count":1}'>
<input type="hidden" name="redirect" value='true'>
<input type="submit">

I want to change the value of count dynamically using jquery.Any help is accepted.

2
  • When you want to change the value of count....On any event or on page redirect or what ? Please specify Commented Sep 9, 2015 at 12:30
  • No way this is not a duplicate of something - search before you post! Commented Sep 9, 2015 at 12:32

2 Answers 2

2

You need to do following steps:

  • Fetch value from text field
  • Parse JSON and store in object
  • Set new value to object
  • Again set value to text as string

Consider following:

var textVal = $("#txtEmail").val();
var obj = JSON.parse(textVal);
obj.count = 2;//as you want
$("#txtEmail").val(JSON.stringify(obj));

DEMO

Sign up to request clarification or add additional context in comments.

5 Comments

Almost close :-) beat you in 25 seconds
@xyz: This is not a race. But if it was, you would have won :)
@D4V1D: Its not a race. I was just knocked out by the identical code post at the same time :-)
@xyz you are knocked off because your answer doesn't say anything but a code block. which might be good but other one is better.
@Jai I agree. I saw that Manwal putting efforts, so, I counted on him :-)
1
$(function() {
  var val = $('#txtEmail').val();
  var valObj = JSON.parse(val);
  valObj.count = 100;//set anything you want
  $('#txtEmail').val(JSON.stringify(valObj));
});

2 Comments

@AbhishekSingh Good Luck :-)
@Manwal :thumbs_up: its a win-win for SO :-)

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.