Joshua Joshua - 3 months ago 16
jQuery Question

Jquery indexOf on data-x

I search for a solution on my Jquery script. I need to create a indexOf on my data-x.

I have a string of several numbers, separated with a ','.

I have one number and i want to check if that number is equal with one of the numbers in the string.

$('[data-showfor="'+answer_id+'"]').hide().removeClass('enquetehidden').fadeIn();


I have tried with a if statement.

if(($(this).val().indexOf(answer_id) > -1)


HTML

<div class="enqueteitem enquetehidden" data-showfor="<?php echo $enquete['depend_answer_ids']; ?>">

Answer

You can use a contains selector. The * let you search for any existance of the answer_id in the attribute data-showfor

var elements = $('[data-showfor*="' + answer_id + '"]');
Comments