0

I have a strange issue, I'm using jQuery to append some HTML to an h1 tag. I'm using a CMS here at work that I don't have access to the backend, so I have to do some trickery to make things work. My code I'm using is...

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

<script type="text/javascript">
$(function() { 
var html = '<span style = "font-size: 10pt; font-style: italic; display: block;">Tools to help make your farm profitable and successful</span>';
$('.mainContent h1').first().html(html); });
</script>

Now I've tested this script on jsfiddle and it works just fine, it appends to the H1 tag and everything looks proper. When the actual page loads it, however, it's treating the entire html variable as a string and it's actually printing out the HTML (tags and all) instead of rendering it as you'd expect.

You can see what I mean here

I've attempted many different variations of it, but still can't seem to get it to work on the page. Any ideas? Thank you in advance.

8
  • The code you've posted seems to work, even through the console on your site. You'll have to be more precise with what exactly you're doing, as it stands now it's not reproducible Commented Jun 27, 2018 at 18:50
  • "I'm using a CMS here " This is probably what's sanitizing the input and converting your HTML to HTML entities Commented Jun 27, 2018 at 18:51
  • You're not actually calling append() in your sample code here, either. But yes, I'd imagine that wherever you're entering this script, at least the < chars are being converted to entities. Commented Jun 27, 2018 at 18:52
  • View source on the page, you'll see <script type="text/javascript">// $(function() { var html = '&lt;span style = "font-size: 10pt; font-style: italic; display: block;"&gt;Tools to help make your farm profitable and successful&lt;/span&gt;'; $('.mainContent h1').first().append(html); }); Commented Jun 27, 2018 at 18:53
  • try encoding your string like this: &#x3C;span style = &#x22;font-size: 10pt; font-style: italic; display: block;&#x22;&#x3E;Tools to help make your farm profitable and successful&#x3C;/span&#x3E; Commented Jun 27, 2018 at 18:53

3 Answers 3

1

Your CMS is likely interfering when it detects the angle brackets in the string. Here's an alternate way of creating DOM elements with jQuery which doesn't use any angle brackets:

$(function() { 
    var span = $(document.createElement('span')).text('Tools to help make your farm profitable and successful').css({
            display: 'block',
            fontSize: '10pt',
            fontStyle: 'italic',
        });
    $('.mainContent h1').first().empty().append(span)
});

The above should work, assuming you want to eliminate the text Become Ag Savvy from the page.

If you want to keep it, and merely append the new span to it, delete the call to .empty().

This is, of course, entirely contingent on the CMS keeping the <script> tags intact (and not converting them to &lt;script&gt;).

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

1 Comment

That worked perfectly! Thank you Pete! I should have thought of that but they have me doing a million other things too. Thank you so much!
0

You can try and sneak around the CMS' encoder by using plain vanilla JavaScript for most of the code:

var elem = document.createElement('span');
elem.setAttribute("style","font-size: 10pt; font-style: italic; display: block");
elem.innerHTML = 'Tools to help make your farm profitable and successful';
$('body').append(elem);

Comments

0

Have you tried using document.createElement() to create your span tag? Something like:

<body class="mainContent">

    <h1></h1>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

    <script type="text/javascript">
    $(function() {

        var el = document.createElement('span');
        el.style.cssText = "font-size: 10pt; font-style: italic; display: block;";
        el.innerHTML = "Tools to help make your farm profitable and successful";

        $('.mainContent h1').first().html(el); 

    });
    </script>

</body>

Comments

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.