whats the best way to go about adding jquery variables in the source inside a widget?
not sure if this is what your thinking but you could use http://codex.wordpress.org/Function_Reference/wp_localize_script
- Referred more than 2000 users
- Power Elite Author: Sold between 2 000 000 - 4 999 999 dollars
- Author was Featured
- Item was Featured
- Austria
- Bought between 100 and 499 items
- Has been a member for 4-5 years
- Interviewed on the Envato Notes blog
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/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
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/
