最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - how to add a h1 inside a div using javascript - Stack Overflow

programmeradmin6浏览0评论

I'm writing a HTML where I want to populate a h1 using Javascript. Here I'm able to populate text, but not the h1. Below is my code.

function getTheIncident(keyWord, text) {
    showTheDiv();
    console.log(keyWord + "\t" + text);

    var e = document.getElementById('textResult');
    e.innerHTML = text.replace('\^', '\'');
}

and this gives me the output as shown in the picture below.

MY aim is to add a h1 with content as Description. Basically like

<h1>Description</h1>

just above A procurement order has been placed for a RAM with 128 Gig

please let me know on how can I do this.

I'm writing a HTML where I want to populate a h1 using Javascript. Here I'm able to populate text, but not the h1. Below is my code.

function getTheIncident(keyWord, text) {
    showTheDiv();
    console.log(keyWord + "\t" + text);

    var e = document.getElementById('textResult');
    e.innerHTML = text.replace('\^', '\'');
}

and this gives me the output as shown in the picture below.

MY aim is to add a h1 with content as Description. Basically like

<h1>Description</h1>

just above A procurement order has been placed for a RAM with 128 Gig

please let me know on how can I do this.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Oct 4, 2017 at 6:55 user3872094user3872094 3,3519 gold badges38 silver badges78 bronze badges 2
  • show us your html. – Matt Greenberg Commented Oct 4, 2017 at 6:58
  • Please click the <> and create a minimal reproducible example – mplungjan Commented Oct 4, 2017 at 6:59
Add a ment  | 

4 Answers 4

Reset to default 3
 var h1= document.createElement('H1');
   h1.innerHTML = "Description";

now add this element to a particular node you want

Just wrap the code to be added as HTML into an h1:

function getTheIncident(keyWord, text) {
    showTheDiv();
    console.log(keyWord + "\t" + text);

    var e = document.getElementById('textResult');
    e.innerHTML = "<h1>" + text.replace('\^', '\'') + "</h1>";
}

Create your tag by using createElement(), then prepend() it to your container where you want to have on first. Check below snippet for reference.

var h1Tag = document.createElement('H1');
h1Tag.innerHTML = "Your Title";
$('.output').prepend(h1Tag);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="output">
  This div contains the output..
</div>

https://fiddle.jshell/LnkfLrev/1/ Something like this? you can use document.getElementById('placeholder').innerHTML = text; to set the text of you h1element

EDIT:

https://fiddle.jshell/LnkfLrev/2/

Sorry, I did not quiet get you question. I updated the fiddle so it works as an anwer

I created an H1 element with var h = document.createElement("H1");

then i set the Text and add it to the body

  h.innerHTML = text;
  document.body.appendChild(h);

hope this helps now

发布评论

评论列表(0)

  1. 暂无评论