-5

I have this function works well when i click the first time but when i click the second time receive a error in this part

eval("var " + data + "=sinvnword;");

Code:

$('a.play-video').click(function(){
  var currentLocation = window.location;


  var fullurl = window.location.href; 
  url =  fullurl.split("sendx")[1];

  var sinvnword = $('.question-label.active').last().text();

  console.log(sinvnword);
  var data =url ;
  eval("var " + data + "=sinvnword;");

  console.log(AXY + "GETIT");
  console.log(AXY+"mor");

  console.log(AXY + "muerte"); 
});
Jacob
  • 77,566
  • 24
  • 149
  • 228

1 Answers1

0

Rather than using eval to create dynamic variable names, simply use dynamic property names:

var data = {};

$('a.play-video').click(function(){
  var currentLocation = window.location;

  var fullurl = window.location.href; 
  var key = fullurl.split("sendx")[1];

  var sinvnword = $('.question-label.active').last().text();

  console.log(sinvnword);
  data[key] = sinvnword;

  console.log(AXY + "GETIT");
  console.log(AXY+"mor");

  console.log(AXY + "muerte"); 
});
Jacob
  • 77,566
  • 24
  • 149
  • 228