3

I have a <button> in order to scroll the page to a specific DIV when clicked (#contact)

<button onclick="location.href='#contact';">Click Me</button>

How to define it in Javascript?

$('button[onclick^="#"]').on('click',function (e) {
// Some stuffs here...
});

I'm using this to animate the scrolling from a button to a DIV:

$('a[href^="#"]').on('click',function (e) {
.preventDefault();
var target = this.hash,
$target = $(target);
$('html, body').stop().animate({
'scrollTop': $target.offset().top
}, 900, 'swing', function () {
window.location.hash = target;
});
});

But it works only on a hrefs because of its a[href^="#"]. So I'm interested to make it work on my <button>s

7
  • 1
    Can't you do this with an anchor element and no JS at all? Commented Sep 11, 2013 at 12:14
  • I don't want to duplicate my button's style to a a tag, also I'm interested to learn sth new instead of using <a href..> Commented Sep 11, 2013 at 12:16
  • I don't understand what you are asking for. Do you want to bind the event handler without jQuery? Commented Sep 11, 2013 at 12:19
  • Your question says 'Define a button's on click event'. If that's what you want to know, something along lines of $('button').click(function(e){//logic}) would work (which already seems there in your question). Please be a little more elaborate if that is not what you want. Commented Sep 11, 2013 at 12:22
  • I added more information to the question, so is it understandable now or need more explain? Commented Sep 11, 2013 at 12:27

5 Answers 5

3

Define the button(s)

<button class="jumper" data-href="#contact">Click Me</button>

Attach the click event.

$('button.jumper').click(function (e) {
    // new location
    location.href= $(this).data().href;
});
Sign up to request clarification or add additional context in comments.

5 Comments

You're using jQuery there. Pure JS: object.onclick=function(){SomeJavaScriptCode};
@ArthurKovacs Hmmm... what if the title of this post would say: "Define a button's onclick event in Jquery"? oh... guess what... it is the title of this post. Joking. Of course the straightforward way using pure old JS is the best, but he asked for JQ.
Yeah, "... event in Jquery", apparently I missed the title and the first line from the question itself put me in JavaScript mode :) . No prb.
But it's impossible to add the Jquery scroll animation to this pure JS method.
You replace my entire click handler with your click handler from your question. Just need to change this line $target = $(target); to this line $target = $($(this).data().href);
0

You can also do it with anchor tag <a>

Top <a href="#bottom">Go to Bottom</a>

<div id="bottom">
Bottom
</div>

Refer above sample code

Comments

0

Html

<button id="clicker" onclick="location.href='#contact';">Click Me</button>

JQuery

$('#clicker').click(function (e) {
  $.scrollTo($('#somediv'), 300);
});

1 Comment

Should the #somediv be same as #contact? I couldn't make it work.
0

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?

Comments

0

Try this:

HTML:

<input type="submit" value="submit" name="submit" onclick="myfunction()">

jQuery:

<script type="text/javascript">
 function myfunction() 
 {
    // do whatever you want
 }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.