1

Is this possible, my div data attribute is:

data-obj="{"__type":"GalleryImgModel","GalleryImageId":49,"GalleryId":0,"PersonId":264}"

and the jquery I tried based on the docs (Ive already defined item correctly!)

item.data('obj',{"MoreThanOneImage" : moreThanOneImage});

Help would be great thanks

1
  • 2
    the data-obj attribute value is not correct as the string enclosing quotes are messing with quotes of json attributes Commented Jul 19, 2013 at 10:59

1 Answer 1

1

You need to convert the data attribute string to an object first, then add the value(s). Try something like:

$('#my-element').data($.extend({'new-key':'new-value'},
    JSON.parse($('#my-element').data()));

Something like that should do the trick.

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

2 Comments

Thanks I used $.extend(item.data('obj'), { "MoreThanOneImage": moreThanOneImage }) and it worked fine
Glad to hear that it helped! Normally I'd say to be careful with the order of your arguments to $.extend and to remember that it's mutating state in-place -- but here it doesn't really make a difference.

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.