Skip Navigation

[Gelöst] Pass a dynamic ID in the Views javascript field

This support ticket is created vor 7 Jahre, 9 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 3 Antworten, has 3 Stimmen.

Last updated by Kris Haamer vor 7 Jahre, 9 Monate.

Assigned support staff: Luo Yang.

Author
Artikel
#294989
Captura de tela 2015-04-10 20.36.07.png

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'); 
  } 
});
#295006

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>
#295262

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.

#296412

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(); 
  	}    
  });
});