santa banta santa banta - 1 month ago 11
HTML Question

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

I have a query. I want to insert text to a 'Paragraph'
tag, but here 'Paragraph' tag doesn't have any ID. So, using ID of body tag I want to insert text. So, I am confused here, please help me here. Following are 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 p tag. So, please guide me here. Thank you.

Edit: I want that text to come from another variable. So, please tell me is it possible using jquery?

Answer

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>

Comments