How To Access HTML5 data- Attributes With JavaScript/JQuery

In HTML5 data- Attributes were introduced. They are a big improvement, because they allow you to store information within a HTML tag. They look like this:

<div id="abc" data-listsize="20"></div>

You see, the div tag is extended by a data-listsize attribute, which has the value 20. But how can you access this attributes with JavaScript? In this article I’ll show you both ways – with plain JavaScript and with jQuery.

Read more

The JSON Format Explained

The JSON format is a very popular format for exchanging data. It is very easy to use. Find out, how to use the JSON format. What is JSON? JSON stands for JavaScript Object Notation. The format is identical to the way, you would create objects in JavaScript. It is a great way to exchange data … Read more

PHP: Convert An Array To JSON

Sometimes, when programming in PHP, you’ll need to convert an array to JSON. This enables you e.g. to retrieve data serverside through PHP and pass it to the client, which then can process this data with javascript. There are different ways to do that, e.g. via AJAX.
In this article I’ll show you, how easy it is to convert a PHP array into a JSON String.

Read more

JavaScript: Get A Random Item From An Array

In one of my previous posts I’ve shown you how to get a random item from an array in PHP. Well, there is, of course, a way to do this in JavaScript as well. And it’s also very easy. Just declare someting like this:

<script>
var allItems = ['A', 'B', 'C', 1, 2, 3, 4, 5];
</script>

Read more

How To Load A Default Image In HTML, If Image Is Not Found

When creating HTML pages, it can always happen, that you include images into a page that can not be displayed. There are several reasons for this, for example when there is a typo in the source code or the image file has been moved or deleted. In this case, using the JavaScript event handler onerror … Read more

How To Alter Background Colours Of HTML Table Rows With jQuery

If you have long lists of data displayed as tables, it may be a bit confusing to read a line across its columns. But there is an easy way to make your tables more readable. Just alter the background-colour of your list line-by-line. In this post I’ll show you how to do this by using … Read more