February 9

jQuery data attribute

I believe that most of you are familiar with jQuery data() function. A lot of things can be done in convenient way using this functionality. I will show you one example. Let’s assume that you are creating news list widget. Content of this widget is based on category id. So there are two elements, category links and news list box.
example
Each of category link has jQuery click handler attached to itself. Click event triggers news box refresh.
example2
Let’s examine code behind this:

Each link has refresh-news class so that single event can be attached. Category id is stored using data-catid attribute. jQuery has its own mechanism for parsing data attribute so it is enough to call $(this).data(‘catid’) inside event handler.

Please follow and like us:

Tags: ,

Posted February 9, 2016 by mmedojevic in category "JavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *