Last Updated: February 25, 2016
·
1.242K
· benorudolf

HTML5 data attribute & javascript

Sometimes we write strings of messages in our javascript code, which isn't really the best practice and could prove an issue, specially if translations are involved (e.g. PHP).

With the HTML5 data attribute we can remedy this and even use the capability for other things.

Rough example:

JAVASCRIPT (jQuery)

// Loading phase 
element.html(element.attr('data-loading'));

// Success phase
element.html(element.attr('data-success'));

// Error phase 
element.html(element.attr('data-error'));

HTML

<p class="msg" data-loading="Getting your stuf..." data-success="Ok, you're all set" data-error="Oh no! Something wen wrong">Messages...</p>

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside