HTML
<div id="code1" data-code="123;12"></div>
<div id="code2" data-code="231"></div>
Jquery/Javascript
alert($("#code1").data("code").split(";")[0]);
alert($("#code2").data("code").split(";")[0]);
alert('test');
Since code2 does not have a ";", the code stops working all together. The last alert will not work nor will any code after the non-splitable code. How can I split code by ";" even when it may not have the ";" character?
data()
will typecast a value to number if it is numeric
Try:
$("#code2").data("code").toString().split(';')
More about typecasting in the html 5 attributes section of data() docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With