1

I got some aspect ratio value in a data-attribute like

<li data-ratio="4/3">4:3</li>

On a click I need to get the correct value - not the string, like I get it with this:

'click li': function(event) {
    var ratio = $(event.currentTarget).attr('data-ratio');
}

I don't know if I have to change the format of the data-attribute or just do some convertion on the result - like:

var value = $(event.currentTarget).attr('data-ratio').split('/');
ratio = value[0] / value[1];
user3142695
  • 15,844
  • 47
  • 176
  • 332
  • 1
    Have you tried it? Did it give you what you were expecting? Are they ALWAYS going to be division? – krillgar Dec 07 '15 at 20:41
  • If you're the one controlling the `data-ratio` value, wouldn't it be wiser to encode is as a number? – Zirak Dec 07 '15 at 21:26

2 Answers2

0

I can not good understand what you are looking for, can you please check this?

$(document).ready(function(){
  $('li').click(function(){
    alert($(this).attr('data-ratio'))
  })
})

Have a look at this fiddle and tell me what you need if this is wrong.

https://jsfiddle.net/

Franco
  • 2,309
  • 1
  • 11
  • 18
0

The .split() method will return an array of strings. Although JavaScript will sometimes handle the type coercion, you should still convert these strings to numbers. You can use the parseInt() function in order to retrieve the number from the string:

Example Here

$('li').on('click', function(event) {
  var values = $(this).attr('data-ratio').split('/');
  var ratio = parseInt(values[0], 10) / parseInt(values[1], 10);

  // ...
});
Josh Crozier
  • 233,099
  • 56
  • 391
  • 304