Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text to <p> tag by using ID of body tag in javascript

I want to insert some text in a paragraph tag, but here the paragraph tag doesn't have an ID. So, using the ID of the body tag I want to insert text. How can I do that?

Following is my HTML code:

<html>
    <head xmlns="http://www.w3.org/1999/xhtml">
        <meta http-equiv="X-UA-Compatible" content="IE=7">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body dir="ltr" id="tinymce" class="mceContentBody " contenteditable="true">
        <p><br data-mce-bogus="1"></p>
    </body>
</html>

Now, I want to add text using JavaScript in the p tag.

I want that text to come from another variable. Is it possible using jQuery?

like image 697
santa banta Avatar asked Feb 19 '15 07:02

santa banta


1 Answers

JavaScript solution: To add text in the first p tag.

 document.getElementsByTagName("p")[0].innerHTML="Whatever text!";

Snippet:

var list = document.getElementsByTagName("p")[0].innerHTML="Whatever text!";
//alert(list);
<html>

<head xmlns="http://www.w3.org/1999/xhtml">
  <meta http-equiv="X-UA-Compatible" content="IE=7">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>

<body dir="ltr" id="tinymce" class="mceContentBody " contenteditable="true">
  <p>
    <br data-mce-bogus="1">
  </p>
</body>

</html>
like image 182
Ani Menon Avatar answered Sep 22 '22 14:09

Ani Menon