---
title: "How to do IndexedDB API"
url: https://blog.creatuwebpymes.com/en/indexeddb-api-local-database/
date: 2025-03-10
modified: 2025-10-16
author: "franciscobritoda69"
image: https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/base-de-datos-indexeddb-square.webp
categories: ["IndexedDB API"]
type: post
lang: en
---

# How to do IndexedDB API

## IndexedDB: the local database in HTML5

This article is about the (https://www.w3.org/TR/IndexedDB-2/), which is a local database on your computer. This API has become the standard in applications made with HTML5.

It has a number of advantages over traditional databases. One of them is that it does not have to connect to the server to return information, so the response is faster. On the other hand, it offers greater flexibility when storing data, as it is not governed by tables.

Let's look at an example where we will create a local database on a web page. The user will be able to insert books with the following parameters: title, author, year and ISBN. Once this data is stored, the user can delete a book from the database if they wish.

First, we will create an HTML document, to which we will add this form in the body of the HTML document. The complete code is below and does not contain CSS styles. This is what will be seen in the browser.

HTML

```

Exercise Module indexedDB

**

## Exercise 6 indexedDB

| Title | |
| --- | --- |
| ISBN | |
| Author | |
| Year | |
| |

Title:

```

!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/formulario-de-ejercicio-de-indexedDB.webp)

## Create the Database in IndexedDB

We have added JavaScript code to the HTML document mentioned above. This JavaScript code will be responsible for creating the local database and storing data in the browser.

Therefore, the first thing we will do is declare the variable bd with global scope. This will allow us to use it inside and outside the functions that we will see below.

We will set the window** object to listen for the **load** event. This load event will be executed once the page has been loaded by the browser. Once this has happened, the **start** function is activated. This line of code is placed at the end of the JavaScript document.

**window.addEventListener(“load”, iniciar, false)**

Next, we declare the variables that correspond to the elements of the HTML document, such as title, ISBN, author, etc. The id value of each element is taken into account for selection.

With **indexedDB.open(“Books”, 2)**; we are creating the Books database in version 2. This is stored in the request variable.

JS

```
//declaration of variable bd

var bd;

function start(){

var title= document.getElementById('title');
var isbn = document.getElementById('isbn');
var author = document.getElementById('author');
var year = document.getElementById('year');
var button = document.getElementById('button');
var delete = document.getElementById('delete');

button.addEventListener('click', add, false);
delete.addEventListener('click', deleteClave, false);

var request = indexedDB.open('deusto', 2);
request.onsuccess = function(e){
bd = e.target.result;
}

request.onupgradeneeded = function(e){
bd = e.target.result;
bd.createObjectStore('books', {keyPath: 'title'});
}
}
```

## Adding and Deleting Data in IndexedDB

The add function collects the values entered by the user and stores them in the database.

JS

```
function add(){

var title = document.getElementById('title').value;
var isbn = document.getElementById('isbn').value;
var author = document.getElementById('author').value;
var year = document.getElementById('year').value;

var book = {title: title, isbn: isbn, author: author, year: year};

var customerObjecstore = bd.transaction('books', 'readwrite').objectStore('books');
customerObjecstore.add(book);
alert ('Register stored successfully in the database');
return false;
}
```

 

The deleteClave() function is responsible for deleting the data from the database for the key designated by the user.

JS

```
function deleteClave(){

var delete = document.getElementById('deleteClave').value;

var customerObjecstore = bd.transaction('books', 'readwrite').objectStore('books');
var register = customerObjecstore.delete(delete);
register.onsuccess = function(e){
alert('Register deleted from the database successfully');
return false;
}
}

window.addEventListener('load', start, false);
```

 

This article is intended for programming students, and one of its exercises involves using IndexedDB as a local database.

In short, there are many more articles and tutorials on the web that discuss (https://amzn.to/3V6c6HC) databases. For those who want to learn more about indexeddb, you can follow the link that specifically discusses this HTML API Creatuwebpymes is a web design and programming company in the Canary Islands that ❤️ programming.

#### Other Posts (/blog/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/palindromo-backtofront-post-blog.webp)](/en/check-palindromes-with-javascript-code/)

#### (/en/check-palindromes-with-javascript-code/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/Calculadora-con-codigo-python-blog.webp)](/en/calculator-python-code/)

#### (/en/calculator-python-code/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/dibujar-triangulo-con-javascript.webp)](/en/triangle-with-javascript/)

#### (/en/triangle-with-javascript/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/base-de-datos-indexeddb-square.webp)](/en/indexeddb-api-local-database/)

#### (/en/indexeddb-api-local-database/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/poligonos-en-python-figuras-geometricas.webp)](/en/how-to-create-polygons-with-python/)

#### (/en/how-to-create-polygons-with-python/)

[!(https://blog.creatuwebpymes.com/wp-content/uploads/2025/03/python-comprehensions-blog.webp)](/en/comprehension-in-python/)

#### (/en/comprehension-in-python/)
