0

I have a string which is basically an HTML String like this -

  var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"

I m trying to convert this HTMLString to real HTML Components using this below jQuery function and placing it in the div id sampleSection like this -

$('#sampleSection').html(str);

But it is still loading as normal string, it does take html elements into consideration but places it as normal string like this -

Type 3 Heading Some text goes in here 

I want it like this -

Type 3 Heading

Some text goes in here

Note - I am trying to load this on iPad InAppWebView with local HTML and script files. Is there something I m missing or do I need to do it differently? Thanks for your help.

var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"
$("#sample").html(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="sample"></div>

6
  • 2
    Your HTML is invalid; your <h3> tag is followed by </h>, which will be ignored. Commented Jul 15, 2020 at 12:37
  • I have corrected it now. It was a typo. Sorry. Commented Jul 15, 2020 at 12:42
  • 1
    it's work with close tag. Commented Jul 15, 2020 at 12:42
  • @nOObiOS have you tried the fixed typo in your actual code? Commented Jul 15, 2020 at 12:43
  • 1
    @nOObiOS OK click the code snippet above. It seems to work just fine. Commented Jul 15, 2020 at 12:47

1 Answer 1

1

The problem is in the closing tag of h3.. You have closed it as ...

var str =  "<section id='section1' class='class1'> <h3>Type 3 Heading</h3><p>Some text goes in <span> here</span> </p></section>"
Sign up to request clarification or add additional context in comments.

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.