05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The DIV tags don’t have to deal with the HREF attribute. Aside from that the two tags will<br />

produce identical results. However, for this to be true within our script there are a few things<br />

you should know about the tag.<br />

Dealing with A and HREF<br />

The tag has the infamous HREF attribute which is used to specify the destination of the<br />

hyperlink. But we don’t want a button created using the tag to go to a web page. In other<br />

words, we want our universal button to be able to respond to a click event. But we don’t want<br />

to do that using the onclick attribute either. After all, this is a <strong>jQuery</strong> tutorial and <strong>jQuery</strong> offers<br />

the convenience of using events which are accessible within a single point in our JavaScript<br />

code. Being able to access important code from a single point in our script will save time<br />

working on our application in the long term.<br />

<strong>jQuery</strong> allows us to bind events to our buttons and this code will always be stored in the same<br />

place during initialization of our web page (let’s say within the $(document).ready event.) This<br />

will prevent us from having to jump from one onclick element to another throughout the entire<br />

HTML part of our web application. And when the time comes to deal with modifying button<br />

functionality in the future we can always refer to that single place in our code to modify<br />

button functionality. And save ourselves a headache.<br />

Let’s now talk more about the HREF attribute. As we just discussed we will be using <strong>jQuery</strong><br />

events instead of onclick and HREF attributes. This means that we need to figure out a way to<br />

completely disable HREF.<br />

You probably already know that when instead of providing a URL we can use the number<br />

symbol # followed by a custom anchor name (like #Paris) as HREF value. This will make the<br />

vertical scrollbar “jump” to the anchor location named “Paris” on your website. In other words<br />

Clicking on this link: Go to paris the page will jump to a location<br />

marked by this code:<br />

Travel to Paris<br />

We can simply use just the # symbol as in Return to top of the page and this<br />

way, seeing how the anchor name is not provided at all, the web page will jump back to the<br />

very beginning.<br />

This is the default hyperlink functionality. But we want this to be completely disabled for our<br />

button. We don’t want the button to go to any URLs or anchors. And we definitely don’t want<br />

our button to jump back to the very top of the page when it’s clicked. But we do want something<br />

to happen when a button is clicked using a <strong>jQuery</strong> event. We’ll discuss that momentarily.<br />

For now let’s disable the HREF functionality completely by doing one of the following:<br />

130

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!