I'm wondering if I can set the value of an HTML object's attribute as a string that contains #
character ?
The reason I want to do this is the page will have a lot of items that should scroll the page to specified elements, and I want to store the data ' which item should it scroll to? ' as a data-scrollto attribute.
So my JavaScript code -will- look like this:
function ScrollToElement(lm)
{
var theTop = lm.offset().top;
$("html,body").animate({
scrollTop: theTop/*,
scrollLeft: 1000*/
});
}
// .. and add click event to all such objects :
$('.scroller').click(function(){
var theSelector = $(this).attr('data-scrollto');
ScrollToElement($(theSelector));
});
so the html elements will look like :
<a class='scroller' data-scrollto='p#p-to-scroll'>Click to scroll to p</a>
is it safe ?
And as a side question, why does
$(element).data('scrollto');
does not work but
$(element).attr('data-scrollto');
works ?