Toggle Text of HTML Element with JavaScript onclick

In this tutorial we will see how to Toggle Text of HTML Element with JavaScript onclick. The onclick event is used with HTML Dom id property, getElementsByName method and getElementsById method.


HTML Code is given below, This code contains button element with onclick event and a paragraph text.

<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Toggle Text of HTML Element with JavaScript onclick</title>
<p id="text"></p>
<button name='button' id='1' onclick="changeText()">Change Text</button>

onclick Event

JavaScript Code is given below, In this code the onclick event is used to toggle the text inside paragraph element.

The onclick event occurs when an element is clicked, it is used to call a JavaScript function when click event is occurred.

In this simple example, the onclick event calls a main function, then id of button element is checked, if id is 1 then text of paragraph is changed to 'Learn Programming' and the id of button is set to 0.

On next click, the id of button will be 0, as it was changed on first click, so the lines inside else will be executed and text will change back to ''.

HTML DOM id Property, getElementsByName method and getElementById method are also used in this example.

HTML DOM id Property

The HTML DOM id Property sets or returns the id of selected html element. In this example it is used to first read the id of button element and then to change it.

getElementsByName method

The getElementsByName method returns the html element with defined value of name attribute. In this example the name is 'button'. It is used to select the button element to read it's id.

Since we are changing the id of button again and again so we can't use it to select it.

getElementById method

The getElementById method is used to select html element with specific id, in this example we are using it to select the paragraph element to change it's text.

JavaScript Code

Take a look at the code given below.

function changeText()
var id=document.getElementsByName("button")[0].id;
document.getElementById("text").innerHTML = "Learn Programming";
document.getElementById("text").innerHTML = "";  

Video Tutorial

Watch video tutorial and demo on Toggle Text of HTML Element with JavaScript onclick.

Count Number of Rows in Table using JavaScript Move Background Image with Mouse Using JavaScript Disable Text Selection Using JavaScript Get Selected Radio Button Value On Click using JavaScript Get input Element Value On Click Using JavaScript Get All Options In Select Tag using JavaScript Enable and Disable Button using JavaScript Get Class of Clicked Element Using JavaScript Toggle Text of HTML Element with JavaScript onclick Call Multiple Functions on one click using JavaScript onclick Check UnCheck all Checkboxes with JavaScript Open Select File Dialog Box Using JavaScript Display Message When File Is Selected Using JavaScript Get Current Page URL using JavaScript Get ID of Clicked Element using JavaScript How To Delay JavaScript Function Call Change Background Color onclick using JavaScript Change Class of HTML Tag Using JavaScript Get Clicked HTML Element Name Using JavaScript Execute a Function on Hover Using JavaScript Delete HTML Element using JavaScript Change HTML Attribute Value using JavaScript Replace a Word from everywhere in a String using JavaScript Show and Hide Element On Click Using JavaScript Passing JavaScript Value from one Page to another T-Rex Run Game Hacked - Hack T Rex Run Game How To Use JavaScript Function as Variable Display Current Date and Time Using HTML and JAVASCRIPT Get Unicode Value of Pressed Keyboard Key using JavaScript Detect Enter Key Press Using JavaScript Hide and Show Element on Enter Key Press Get Mouse Cursor Position in Pixels using JavaScript Open Link in New Window using JavaScript Open Link in New Tab using JavaScript Get Image Width and Height using JavaScript Remove Last Digit of Number using JavaScript Bitwise OR Simple JavaScript Calculator Using Vanilla JavaScript Disable Right Click on Website Using JavaScript How To Add Line Breaks in JavaScript Alert Box Add Class to HTML Tag On Click using JavaScript