1

I want to change sub element of below data attribute

<div class="blue-shape"
data-actions='[{"event":"mouseenter","action":"jumptoslide","slide":"rs-18","delay":""}]'>

for this i have added below jquery code but it doesn't work

$(document).ready(function(){
    jQuery('.blue-shape').attr("data-actions",{event:'mouseenter', action:'jumptoslide', slide:'rs-16',delay:''});
});

.blue-shape is div class name where i want to change data attribute

2
  • 2
    jQuery.attr expects second parameter to be string Commented Jul 11, 2016 at 10:10
  • The attribute's just encoded as JSON. Commented Jul 11, 2016 at 10:12

4 Answers 4

2

You can pass a function as a second arguement and you can iterate over to change any value like:

jQuery(document).ready(function($) {
  console.log($('.blue-shape').data('actions'));
  
  $('.blue-shape').attr("data-actions", function() {
    var arr = $(this).data('actions'), newArr = [];
    $.each(arr, function(i, obj){
       if(obj.slide === "rs-18"){
          obj.slide = "rs-16"
       }
       if(i === arr.length-1){ newArr.push(obj); }
    });
    return newArr;
  });
  
  console.log($('.blue-shape').data('actions'));
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="blue-shape" data-actions='[{"event":"mouseenter","action":"jumptoslide","slide":"rs-18","delay":""}]'></div>

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

Comments

0

jQuery.attr() expects second parameter to be string.

have a look at jQuery.data() also

$('document').ready(function() {
  jQuery('.blue-shape')
    .attr("data-actions", "{event:'mouseenter', action:'jumptoslide', slide:'rs-16',delay:''}");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="blue-shape" data-actions='[{"event":"mouseenter","action":"jumptoslide","slide":"rs-18","delay":""}]'>DATA</div>

Comments

0

You need to pass string in 2nd param or you can simply use data() method too like:

console.log($('.blue-shape').data("actions"));
$('.blue-shape').data("actions","[{event:'mouseenter', action:'jumptoslide', slide:'rs-16',delay:''}]");
console.log($('.blue-shape').data("actions"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<div class="blue-shape"
data-actions='[{"event":"mouseenter","action":"jumptoslide","slide":"rs-18","delay":""}]'>

Comments

0

I just updated your jquery with this following code please check and vote if it works

$('document').ready(function(){
jQuery('.blue-shape').attr("data-actions","[{'event':'mouseenter', 'action':'jumptoslide', 'slide':'rs-16','delay':''}]");
});

For Reference

https://jsfiddle.net/jasonantho/6ehmded3/

4 Comments

sorry! this gives error SyntaxError: unterminated string literal
Have u checked the fiddle it works fine no error is thrown please check the fiddle
i'm using jquery version 1.11.3. so, i need to use another or not??
Ya i hope so i m using 1.12 ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js use this and check thanks @Dipesh

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.