0

If I have a element with multiple values inside a data-attr, how can I add those individually to a array?

<button id="test" data-values="1 13513 51681">TEST</button>

… and this:

$('#test').on('click',function(){
  myArr.push($(this).data('values'));
  console.log(myArr)
});

… gives me: ["1 13513 51681"].

But I need: ["1","13513","51681"]

4 Answers 4

2

You can use split() function that will return you the array straightaway.

$('#test').on('click',function(){
   myArr = $(this).data('values').split(' ');
   console.log(myArr)
});
Sign up to request clarification or add additional context in comments.

Comments

1

Use split() in javascript it gives in array what you expected

$('#test').on('click',function(){
    myArr = $(this).data('values').split(" ");
});

Comments

1

You have not splitted the string so you cant access all values in array, you will get only one element due to that like 1 13513 51681. So use following

$('#test').on('click',function(){
  myArr = $(this).data('values').split(' ');
  myArr.push($(this).data('values'));
  console.log(myArr)
});

Comments

0
var dataarry=[entityid,domainid,'entityid_domainid',filetemplate]
function filetemplateremove(entityid, domainid) {
      var removeItem = entityid + "_" + domainid;                          
      dataarry.splice(dataarry.indexOf(removeItem), 1);
}

1 Comment

You need to explain the code that you have shared. Sharing the code is not enough.

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.