How can I change div content with JavaScript?

I have simple HTML code with some JavaScript. It looks like:

<html>
<head> <script type="text/javascript"> function changeDivContent() { // ... }; </script>
</head>
<body> <input type="radio" name="radiobutton" value="A" onClick="changeDivContent()"> <input type="radio" name="radiobutton" value="B" onClick="changeDivContent()"> <div></div>
</body>
</html>

I just wanted to be able to change the div's content (it's inner html) with selecting one of "A" or "B" radio buttons, but div#content does not have javascript attribute "value", so I am asking how it can be done.

6 Answers

Assuming you're not using jQuery or some other library that makes this sort of thing easier for you, you can just use the element's innerHTML property.

document.getElementById("content").innerHTML = "whatever";
4
$('#content').html('whatever');
2

Get the id of the div whose content you want to change then assign the text as below:

 var myDiv = document.getElementById("divId"); myDiv.innerHTML = "Content To Show";
1

you can use following helper function:

function content(divSelector, value) { document.querySelector(divSelector).innerHTML = value;
}
content('#content',"whatever");

Where #content must be valid CSS selector

Here is working example.


Additionaly - today (2018.07.01) I made speed comparison for jquery and pure js solutions ( MacOs High Sierra 10.13.3 on Chrome 67.0.3396.99 (64-bit), Safari 11.0.3 (13604.5.6), Firefox 59.0.2 (64-bit) ):

document.getElementById("content").innerHTML = "whatever"; // pure JS
$('#content').html('whatever'); // jQuery

enter image description here

The jquery solution was slower than pure js solution: 69% on firefox, 61% on safari, 56% on chrome. The fastest browser for pure js was firefox with 560M operations per second, the second was safari 426M, and slowest was chrome 122M.

So the winners are pure js and firefox (3x faster than chrome!)

You can test it in your machine:

0

change onClick to onClick="changeDivContent(this)" and try

function changeDivContent(btn) { content.innerHTML = btn.value
}
function changeDivContent(btn) { content.innerHTML = btn.value
}
<input type="radio" name="radiobutton" value="A" onClick="changeDivContent(this)">
<input type="radio" name="radiobutton" value="B" onClick="changeDivContent(this)">
<div></div>
<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title>
</head>
<body> <input type="radio" name="radiobutton" value="A" onclick = "populateData(event)"> <input type="radio" name="radiobutton" value="B" onclick = "populateData(event)"> <div></div>
</body>
</html>

-----------------JS- code------------

var targetDiv = document.getElementById('content'); var htmlContent = ''; function populateData(event){ switch(event.target.value){ case 'A':{ htmlContent = 'Content for A'; break; } case 'B':{ htmlContent = "content for B";
break; } } targetDiv.innerHTML = htmlContent; }

Step1: on click of the radio button it calls function populate data, with event (an object that has event details such as name of the element, value etc..);
Step2: I extracted the value through event.target.value and then simple switch will give me freedom to add custom text.

Live Code

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like