1

i want to change specific value of json array. for detail i have this dom :

<input class="fileupload" type="file" data-form-data='{"table_reference": "data_monitoring", "table_token" : "X43sd"}'>         

i know how to update the data attribute using jquery using this code : $(this).attr('key', 'value')

but how to change specific key in data attribute, for example on above dom I need to change the table_token value

thank for any suggestion

1 Answer 1

3

You can use .data() with property reference to set the specific property to a value

$(".fileupload").data().formData.table_token = 123;

console.log($(".fileupload").data().formData);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="fileupload" type="file" data-form-data='{"table_reference": "data_monitoring", "table_token" : "X43sd"}'>

If requirement is to set data-* which will be reflected in HTML you can use HTMLElement.dataset

var new_token = "abc";

let data = JSON.parse($(".fileupload")[0].dataset.formData);

data.table_token = new_token;

$(".fileupload")[0].dataset.formData = JSON.stringify(data);
Sign up to request clarification or add additional context in comments.

6 Comments

I think you mean .data().formData.table_token
@navotera Not sure what you mean? The property is set at the .data() object
please check my fiddle
Yes, the .data() is set at the jsfiddle
sorry table_token value never changed when i click 'save' button
|

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.