701 posts
  • 8 Years of Membership
  • Affiliate Level 1
  • Canada
  • Collector Level 6
chrismccoy
says

whats the best way to go about adding jquery variables in the source inside a widget?

3439 posts
  • Author Level 4
  • Collector Level 2
  • Community Superstar
  • United States
+2 more
organicbee
says

not sure if this is what your thinking but you could use http://codex.wordpress.org/Function_Reference/wp_localize_script

640 posts
  • Affiliate Level 8
  • Author Level 14
  • Collector Level 4
  • Front Page Master
+12 more
Kriesi
says

3 options come to mind:

  • Use a hidden input field
  • Create a script tag and store your variable as javascript var
  • use the html 5 data attribute

If you need to send the data the hiden input is the way to go. If its a lot of data to read I would recommend the script tag. If you only need to store a setting or two I prefer the html5 data attribute for its convenient use with jquery. Any element is allowed to have any number of data attributes in this format: data-key=”value”. You could easily read and set that value with jquerys data method:

//get
var get = $(element).data('key');

//set
$(element).data('key', 'newValue')
http://api.jquery.com/data/
701 posts
  • 8 Years of Membership
  • Affiliate Level 1
  • Canada
  • Collector Level 6
chrismccoy
says
Kriesi said
3 options come to mind:
  • Use a hidden input field
  • Create a script tag and store your variable as javascript var
  • use the html 5 data attribute

If you need to send the data the hiden input is the way to go. If its a lot of data to read I would recommend the script tag. If you only need to store a setting or two I prefer the html5 data attribute for its convenient use with jquery. Any element is allowed to have any number of data attributes in this format: data-key=”value”. You could easily read and set that value with jquerys data method:

//get
var get = $(element).data('key');

//set
$(element).data('key', 'newValue')
http://api.jquery.com/data/

what i mean is taking values from a widget, and having it output as an option for a jquery plugin through wp_head, so it initlizes the values for the plugin being used

variable: 1, variable2: ‘string’,

etc

3439 posts
  • Author Level 4
  • Collector Level 2
  • Community Superstar
  • United States
+2 more
organicbee
says
chrismccoy said
Kriesi said
3 options come to mind:
  • Use a hidden input field
  • Create a script tag and store your variable as javascript var
  • use the html 5 data attribute

If you need to send the data the hiden input is the way to go. If its a lot of data to read I would recommend the script tag. If you only need to store a setting or two I prefer the html5 data attribute for its convenient use with jquery. Any element is allowed to have any number of data attributes in this format: data-key=”value”. You could easily read and set that value with jquerys data method:

//get
var get = $(element).data('key');

//set
$(element).data('key', 'newValue')
http://api.jquery.com/data/

what i mean is taking values from a widget, and having it output as an option for a jquery plugin through wp_head, so it initlizes the values for the plugin being used

variable: 1, variable2: ‘string’,

etc

use exactly what I posted here is a better example of its use http://ottopress.com/tag/wp_localize_script/ & http://www.ronakg.com/2011/05/passing-php-array-to-javascript-using-wp_localize_script/

by
by
by
by
by
by