programminghead Logo

JavaScript Tutorial 2 : Changing HTML Content using JavaScript

Selecting HTML Elements:

we have 2 Different ways for Selecting HTML Elements so we can add/change there Contents.
1. Changing HTML Content by id
2. Changing HTML Content by Tag Name

Changing HTML Content by id :

We can use HTML Element's id to make change in the HTML Element's Content.
to select HTML Element's ID we have to use JavaScript's document.getElementById('') method.

document.getElementById('') Method :

Using JavaScript's document.getElementById('') Method we can select/target HTML's Elements in JavaScript so we can make change in the Element's Content.
here document targets the whole HTML Document and getElementById select the HTML Element of given ID Between the single/double quotes.
But this will only select HTML Element's id, to make change in the Content we have to use .innerHTML property for that.

.innerHTML property

after selecting HTML Element this JavaScript's .innerHTML property defines the HTML Content for Selected HTML Content.

Changing Content by id Example


<html>
 <head>
  <title>Changing Content by id </title>
 </head>
 <body>
   <p id="myparagraph"> </p>
   <script type="text/javascript">
     document.getElementById('myparagraph').innerHTML="text for HTML Element by JS";
   </script>

 </body>
</html>

Results


text for HTML Element by JS



Script Tag <script> :

Script tag is used to define JavaScript inside HTML Document.

type attribute type="" :

Type attribute defines the language of the script or format of the data inside Script tag.

document.getElementById('') method:

Using JavaScript's document.getElementById('') Method we can select/target HTML's Elements in JavaScript so we can make change in the Element's Content.

.innerHTML property :

after selecting HTML Element this JavaScript's .innerHTML property defines the HTML Content for Selected HTML Content.

Changing HTML Content by Tag Name :

We can use HTML Element's TagName to make change in the HTML Element's Content.
to select HTML Element's Tag we have to use JavaScript's document.getElementsByTagName('') method.

document.getElementsByTagName('') Method :

Using JavaScript's document.getElementsByTagName('') Method we can select/target HTML's Elements in JavaScript so we can make change in the Element's Content.
here document targets the whole HTML Document and getElementsByTagName('') select the HTML Element of given Tag Name Between the single/double quotes.
But this will select all HTML Elements if you have more then 1 elements created using same tag, But if you want to make change in only one HTML Element's content then you can create a Element with unique tag or you can give that Element unique id and change content using previous document.getElementById('') method.
if your HTML Document have multiple tag para tag and you still want to target that para tag, than you have to give index number document.getElementsByTagName('p')[0] after writing your tag name, so JavaScript can understand which tag to target. after selecting your HTML Element we have to use .innerHTML property to make change in the Element's Content.

.innerHTML property

work exactly the same for this method, after selecting HTML Element by TagName this JavaScript's .innerHTML property defines the HTML Content for Selected HTML Content.

Changing Content by TagName Example


<html>
 <head>
  <title>Changing Content by id </title>
 </head>
 <body>
    <p> first para </p><br/>
    <p> second para </p><br/>
    <p> third para </p>
   <script type="text/javascript">
     document.getElementsByTagName('p')[0].innerHTML="text of selected paragraph tag";
   </script>

 </body>
</html>

myjavascript.js file


text of selected paragraph tag


second para


third para


Script Tag <script> :

Script tag is used to define JavaScript inside HTML Document.

type attribute type="" :

Type attribute defines the language of the script or format of the data inside Script tag.

document.getElementById('') method:

Using JavaScript's document.getElementById('') Method we can select/target HTML's Elements in JavaScript so we can make change in the Element's Content.

.innerHTML property :

after selecting HTML Element this JavaScript's .innerHTML property defines the HTML Content for Selected HTML Content.

[0] index number after tagName :

in case of Multiple tags index [0] number helps JavaScript to target correct tag.
index number starts from 0 so
[0] is the first tag
[1] is the second tag
[2] is the third tag

Getting all HTML Document's Tag and id Name:

using "*" star parameter we can get all HTML Tags present on the current Document.
for that we have to just write "*" star parameter in the place of id and tag name and print that value in the output screen using document.write() method.

Comments

how to download minecraft free

http://tinyurl.com/yxasva3n
07:21 am

Hi there, I log on to your new stuff daily. Your story-telling style is awesome, keep doing what you're doing!

how to download minecraft

http://tinyurl.com/yyggv7ld
12:06 pm

My brother suggested I might like this blog. He was totally right. This submit actually made my day. You cann't imagine just how a lot time I had spent for this info! Thank you!

g

http://bit.ly/3078WYE
12:28 am

What's up to all, it's in fact a fastidious for me to visit this web page, it contains priceless Information.

minecraft free download pc

http://tinyurl.com/yynevcg8
02:14 pm

I got this website from my buddy who told me concerning this website and now this time I am visiting this web site and reading very informative articles at this place.

g

http://tinyurl.com/yy27l6vd
12:51 pm

Hi are using Wordpress for your site platform? I'm new to the blog world but I'm trying to get started and set up my own. Do you require any coding knowledge to make your own blog? Any help would be greatly appreciated!

g

http://j.mp/2HazGyQ
01:42 pm

I'm gone to convey my little brother, that he should also pay a quick visit this website on regular basis to take updated from latest information.

g

http://bit.ly/2JvwNv7
05:40 am

Thanks for the marvelous posting! I genuinely enjoyed reading it, you're a great author.I will ensure that I bookmark your blog and will eventually come back someday. I want to encourage you to ultimately continue your great job, have a nice afternoon!

download minecraft free

http://tinyurl.com/y3mz7xhe
08:10 am

I do consider all the concepts you have introduced to your post. They are very convincing and will definitely work. Nonetheless, the posts are very brief for starters. May you please prolong them a bit from subsequent time? Thank you for the post.

minecraft for free

http://j.mp/2vHEVkk
12:57 pm

Your way of explaining everything in this piece of writing is in fact good, every one can simply know it, Thanks a lot.

minecraft download for free

http://j.mp/2WnX5TW
05:13 am

Hey there just wanted to give you a quick heads up and let you know a few of the pictures aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different web browsers and both show the same results.

gamefly

http://tinyurl.com/yync3v29
07:11 am

I am truly grateful to the holder of this web site who has shared this wonderful article at at this time.

gamefly free trial

http://tinyurl.com/y3ecm32p
06:38 am

I feel that is among the so much vital info for me. And i am glad reading your article. But wanna commentary on some common issues, The web site taste is perfect, the articles is in point of fact great : D. Excellent activity, cheers

gamefly free trial

http://tinyurl.com/y6c3j9r5
06:20 pm

I have fun with, lead to I discovered exactly what I used to be having a look for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

gamefly

http://j.mp/2LeOly6
02:19 pm

Hello, just wanted to mention, I loved this article. It was inspiring. Keep on posting!

Write to Us