Iftek Har Iftek Har - 10 days ago 6
Javascript Question

How to replace some HTML code in a div with another new HTML code by Javascript

I am using WordPress and I want to replace some HTML on footer. I do not want to create a child theme for it. Just want to use simple JavaScript. Following is the code which is currently on website.

<div class="copyright">
Copyright 2016
<a href="www.myclientwebsite.com" title="My Client Title">
<span>Client Company Name</span></a>.
Powered by
<a href="http://wordpress.org" target="_blank" title="WordPress">
<span>WordPress</span></a>.
Theme: by
<a href="http://thememakerwebsitelink.com/themename" target="_blank" title="Thememaker" rel="theme">
<span>Theme Maker Name</span></a>.




Now I want to change all the HTML code inside the "copyright" div with my new HTML code using JavaScript. My new HTML code will be like it or whatever other.

<div class="copyright">
<div class="mynewdiv">
Copyright 2016
<a href="www.myclientwebsite.com" title="My Client Title">
<span>Client Company Name</span></a>.
Website Designed by
<a href="http://www.mywebsite.com/">My Company Name</a>
</div>




I used a code for this it works but not support HTML.

<script type="text/javascript">document.getElementsByClassName('copyright')[0].replaceWith("Text without HTML");




I want simple JavaScript code where I can simply type my new HTML code.

Please let me know How can I do it.

Answer
var yourHTML = '<div class="mynewdiv">Copyright 2016<a href="www.myclientwebsite.com" title="My Client Title"><span>Client Company Name</span></a>. Website Designed by<a href="http://www.mywebsite.com/">My Company Name</a></div>';
document.getElementsByClassName('copyright')[0].innerHTML = yourHTML;
// If you change your HTML, change the value of yourHTML