701 posts
  • Bought between 1000 and 4999 items
  • Canada
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
chrismccoy says

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

3194 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
  • Exclusive Author
organicbee says

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

615 posts
  • Power Elite Author
  • Referred more than 2000 users
  • Power Elite Author: Sold between 2 000 000 - 4 999 999 dollars
  • Austria
  • Bought between 100 and 499 items
  • Has been a member for 5-6 years
+4 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
  • Bought between 1000 and 4999 items
  • Canada
  • Has been a member for 6-7 years
  • Referred between 1 and 9 users
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

3194 posts
  • Sold between 5 000 and 10 000 dollars
  • United States
  • Bought between 10 and 49 items
  • Has been a member for 4-5 years
  • Exclusive Author
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