JQuery Programmer Question: Download Questions PDF
How does jquery store data related to an element?
Answer:
In plain java scripts the information about an element can be stored by adding a domain object model property to the element. This results in memory leak problems in certain browsers. In jquery the user does not has to worry about memory management issues.
For ex : Storing and retrieving data related to an element:
$('#myDiv').data('keyName', { foo : 'bar' });
$('#myDiv').data('keyName'); // { foo : 'bar' }
Jquery allows the user to store any kind of information on an element. The $.fn.data is used the most to store data related to an element.
For ex : Storing of relationship between elements with the use of $.fn.data :
$('#myList li').each(function()
{
var $li = $(this), $div = $li.find('div.content');
$li.data('contentDiv', $div);
});
// later, the user is not required to look for the div again;
// the data can simply be read from the list items data;
var $firstLi = $('#myList li:first');
$firstLi.data('contentDiv').html('new content');
For ex : Storing and retrieving data related to an element:
$('#myDiv').data('keyName', { foo : 'bar' });
$('#myDiv').data('keyName'); // { foo : 'bar' }
Jquery allows the user to store any kind of information on an element. The $.fn.data is used the most to store data related to an element.
For ex : Storing of relationship between elements with the use of $.fn.data :
$('#myList li').each(function()
{
var $li = $(this), $div = $li.find('div.content');
$li.data('contentDiv', $div);
});
// later, the user is not required to look for the div again;
// the data can simply be read from the list items data;
var $firstLi = $('#myList li:first');
$firstLi.data('contentDiv').html('new content');
Download JQuery Programmer Interview Questions And Answers
PDF
Previous Question | Next Question |
How can an element be checked if it contains a specific class? | How can events be prevented to work after an ajax request? |