jQuery
for your button' s
$('button[onclick^="location.href=\'#"]').on('click',function (e) {
e.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});
A jQuery way to getthe buttons that contain a click event with the value 'location.href=#'
$('button[onclick^="location.href=\'#"]').on('click',function (e) {
// Some stuffs here...
}
Ways to find/create buttons and define onclick event in javascript:
Search the first button (note [0])
document.getElementsByTagName('button')[0].onclick=function(){
location.href='#contact';
}
get the button by id (note 'myButton')
document.getElementById('myButton').onclick=function(){
location.href='#contact';
}
create the whole button dynamically and add it to the body
var button=document.createElement('button');
button.onclick=function(){
location.href='#contact';
}
document.body.appendChild(button);
modern ways to find the button
var button=document.querySelector('button');
var button=document.querySelectorAll('button')[0];
B pure javascript way to getthe buttons that contain a click event with the value 'location.href=#'
var buttons=document.querySelectorAll('button[onclick^="location.href=\'#"]');
non ie events
button.addEventListener('click',function(){
location.href='#contact';
},false);
ie events
button.attachEvent('onclick',function(){
location.href='#contact';
});
any questions?
button's style to aatag, also I'm interested to learn sth new instead of using<a href..>