Iftek Har Iftek Har - 1 year ago 113
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">
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>

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 Source
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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download