jquery - How to save a clicked link's data-attribute for use with other functions in JavaScript -


i'm new jquery , js , trying use chosen "subject" matter when user chooses list of buttons. using subject, data-attribute value, beyond me when using data in , out of scope.

  1. what pros , con's of saving data window object?

  2. best or better ways it...

  3. what pro's , con's of saving , retrieving input field?

  4. are there better ways don't know about?

we'll call link #link data attribute foo

var data; //this function called when link clicked - making sure var *only* created clicked links $('#link').click(function() {   data = $(this).data('foo');   //code whatever want }) 

so have, page, variable data value of foo stored inside it. functions can use data stored in variable, , store somewhere later use using either localstorage or cookie.

if use jquery cookie plugin on github, can call function , set cookie this:

$.cookie('name','value'); 

so in instance:

$.cookie('foo',data); 

alternatively, modern browsers can use localstorage, doesn't require plugins may bit limited in ie , older versions of firefox etc.

usage localstorage be:

localstorage.foo = data; //localstorage.foo can want -- localstorage.bar, etc 

next time want reference it, pull javascript/jquery using either or setting var:

if(localstorage.foo == "this") { //do } 

or

var foo = localstorage.foo; if(foo == "this") { //do something} 

to answer question more directly:

1) browser compatibility. biggest enemy, if know have deal older browsers ie8. pros - it's incredibly easy , flows javascript/jquery

2) provide fallback. in case using cookie prevent losing functionality in older browsers.

3) not sure understand you're asking there.

4) probably. there's newer , bigger , better coming out. these 2 methods use in sites, , haven't had major issues yet.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -