I'm using a view to generate a bunch of cards.
Each card has a unique ID which I need to target with javascript,
however I can't see a way to pass a variable in the view's javascript field.
The js would look something like this. Note the ID "3594" which
I would need to insert dynamically for each card.
jQuery(document).ready(function($) {
if($('.slideBox_3594_standard').length > 0) {
$('.slideBox_3594_standard').Slidebox();
} else {
alert('already exists');
}
});
Hey Krish - you can easily set the post id (for example) as a data attribute in your html - so perhaps:
<div class="entry_media" data-id="[wpv-post-id]">
in your jQuery you can easily pull the data attributes
myClass= $('.entry_media').data('id');
You could also use the id directly in your selectors:
$('.entry_media[data-id=3594]');
And I think even you might get away with:
<a href="[wpv-post-url]" class='slideBox_[wpv-post-id]'></a>
Luo Yang
Supporter
Languages:
Englisch (English )
Vereinfachtes Chinesisch (简体中文 )
Timezone:
Asia/Hong_Kong (GMT+08:00)
Dear Krish,
Does the solution of Simon works? please let me know if you need more assistance for it.
Hi Simon and Luoy, thanks for your suggestions, what I ended up doing was this:
jQuery(document).ready(function($) {
var id;
$('.slidebox').each(function(i) {
console.log($(this).data("id"));
id = '.slideBox_' + $(this).data("id") + '_standard';
if($(id).length > 0) {
$(id).Slidebox();
}
});
});