Html5 data attribute jquery

jqel.css("background-color", color); } This takes a DOM element, and uses jQuery to first extract the value of the data-attribute and then to set it as the background-color in CSS. Let's immediately wrap that in a jQuery event handler, so we can see it in action: $(".highlightable").on("hover", function() {. Set the page up in its initial state Add the event handler to show/hide the containers. We then have a div which also has some properties. First thing to note is that we've started including the word blink into our data attributes. The reason for this is to provide a namespace for our general application, which we're going to call "blink". It means if there are any other data attributes on the page for other libraries, we can avoid our application accidentally tripping over on them. This is general good practice that's worth following. Here we've defined an anchor tag which has three properties, all of which are specific to our application (i.e. they are not special features of HTML): Here's a block of code that implements our initial state set-up: function init () {. data-blink-init="hidden" — this tells us that the content should be hidden initially. jqel.css("background-color", color); }); We've bound a hover event to the elements in the page with class highlightable and we've replaced the explicit pass of element with the use of this as provided by jQuery. If you implement this you'll see immediately that the element changes colour when you hover over it, but not when you move your mouse out again; it'll just remain highlighted. This gives us an excuse to store data in the element using an attribute. We'll do this by binding an event handler to the mouseenter and mouseleave events using jQuery's shortcut: $(".highlightable").on("hover", Our first step is to come up with a function that can read the data-highlight attribute and set the background of the div appropriately. Something like this: function(element) {. To handle the mouseleave event, all we need to do is reverse this process: $(".highlightable").on("hover", The behaviour we want is that when you click the "Show" link, then the related container will appear, and the link will be replaced by "Hide". Then when you click "Hide" the related container will disappear and the link will once again say "Show". Lets build up this code step-by-step. At the end of this post is the full functioning code-snippet. First, set up the framework for the code on the page: } }); The first function is run when the mouse enters the area of the element, and the second function is run when the mouse leaves it. Now we augment our function for setting the highlight to also record the original colour of the element: $(".highlightable").on("hover", } }); When this first function runs, it will first extract the current colour from the CSS of the element, and store it in the element under data-original before then going on to set the highlight. In the DOM, our div now looks like this: data-blink="container" — this tells us that this is the container for the content we want to show/hide. data-blink-id="my-content" — this gives the container an id for us to find it by. } }); We've now extended our second function to read back the value of data-original and to revert the CSS to the original colour. Show and Hide Page Sections Let's do a slightly more complex worked example from a real-world use case that you are bound to run into eventually: showing and hiding page sections based on button clicks. We'll start by imagining what the HTML would look like to allow us to do this generally. Something like: data-blink-controls="my-content" — this tells us the id of the content that the link will show/hide. How the above HTML looks in your browser. Now we'll implement the init function which will work in two parts:. If you're writing JavaScript to work over existing HTML, you can make effective use of the HTML5. attributes to allow your code to be generalised; that is, not written for that one-and-only bit of HTML. There are a number of common libraries that use this technique, and really demonstrate the value of having general code whose behaviour can be customised by the HTML. My favourite example is Parsley, a form validation library which is initialised over your HTML form, and takes all its config and behaviour from the data attributes. First, a quick recap on what data attributes are. HTML5 allows you to add an attribute that starts with data- to any element, which essentially allows you to specify custom — but still valid — attributes. For example:. This is useful because any JavaScript you have on the page can extract information from or store information in those tags. This allows the HTML author to specify information that a generalised JavaScript library can use to determine how to behave on that page. Let's do a couple of worked examples to see this in action. Control how text is highlighted Suppose we want a very simple JavaScript library which can highlight blocks of text when you hover over them, and we want the block of text to tell us what colour to highlight. Maybe this would be useful in a CMS where we want the content writer to have control over the highlight colour. Or maybe the colour of the text comes from a database, and we can't know it in advance to include it in our CSS. Our HTML could be like this: data-blink="controller" — this tells us the anchor tag is something our application will call a "controller", that is, it's going to control the show/hide. If you haven't previously confirmed a subscription to a Mozilla-related newsletter you may have to do so. Please check your inbox or your spam filter for an email from us. You're using an insecure, outdated operating system no longer supported by Firefox. Your system doesn't meet the requirements to run Firefox. Your system may not meet the requirements for Firefox, but you can try one of these versions:. Your system doesn't meet the requirements to run Firefox. Your system doesn't meet the requirements to run Firefox. Your system may not meet the requirements for Firefox, but you can try one of these versions:. Your system doesn't meet the requirements to run Firefox. Your system doesn't meet the requirements to run Firefox. You've already got the Firefox browser. Now get everything else Firefox. Congrats! You're using the latest version of Firefox. Sync bookmarks, send tabs, save to Pocket and more with a Firefox Account. Learn more. Your system doesn't meet the requirements to run Firefox. Your system may not meet the requirements for Firefox, but you can try one of these versions:. Visit Mozilla Corporation's not-for-profit parent, the Mozilla Foundation. Portions of this content are 1998–2019 by individual contributors. Content available under a Creative Commons license.

Schrotpatronenpackungen und einzelne Patronen, die sich für die Taubenjagd eignen Aufgeschnittene Schrotpatrone Rio Sporting