1

Im working on it from many many days (I'm not a developer). I want to replace HTML element with another element/text (p to h1 in the page-id-27), have tried 3 different solutions and are working in inspection mode and 'visually', but the source code doesn't change. I really need that the source code can change too. Is this idea possible? Is something wrong here? Many thanks

HTML:

<p class="title_banner"><strong>Sport</strong></p>

3 Solutions:

$.noConflict();
jQuery(document).ready(function($){
var str = '<h1>Sport</h1>';
$('.title_banner').replaceWith(str);
    });

var e = document.getElementsByClassName('title_banner')[0];
var d = document.createElement('h1');
d.innerHTML = e.innerHTML;
e.parentNode.insertBefore(d, e);
e.parentNode.removeChild(e);
$.noConflict();
jQuery(document).ready(function($){
$(document).ready(function(){
$(".title_banner").html("<h1>Sport</h1>");
});
});

Function

function scriptnew(){
wp_register_script('dynamic-title', get_stylesheet_directory_uri() . '/js/dynamic-title.js', array( 'jquery' ), NULL, false );
if ( is_page('27') ) {
wp_enqueue_script( 'dynamic-title' );
}}
add_action( 'wp_enqueue_scripts', 'scriptnew', 10 );

1
  • 1
    Source code can't be changed with javascript. It is what is sent by server Commented Nov 29, 2020 at 13:26

1 Answer 1

2

Try to replace element

First get innerHtml from element p.

Then create a new element h1 place the content in the new element.

After this replace element p with h1

var x = document.getElementsByClassName('title_banner')[0];
var y = x.innerHTML;

var newItem = document.createElement('h1');
newItem.innerHTML = y;

x.parentNode.replaceChild(newItem, x);
<p class="title_banner"><strong>Sport</strong></p>

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you but it doesn't work. Source code can't be changed with javascript. (see comment above).
Please copy the HTML code from your web page and attach it to your original post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.