Suppose that you have a <p> element with an id of 'id1'. Use javascript to set the inner html of this element to 'hello!'

We will do this in 2 steps to make it easier to understand. Firstly, we need to obtain the actual <p> element with javascript. To do this, we use the following command:var p = document.getElementById('id1'); This finds the element by its id tag, and assigns it to our javascript variable p.Now we need to change the inner html with this command:p.innerHTML = 'hello!'; Once you understand the 2 commands going on here, you can use both of them in one line, which saves space and saves us from having to define any javascript variables:document.getElementById('id1').innerHTML = 'hello!';

JC
Answered by Jamie C. Javascript tutor

1219 Views

See similar Javascript Mentoring tutors

Related Javascript Mentoring answers

All answers ▸

If I do var a=3 and var b=4 and then do var c=a+b it tells me the answer is 34. Why?


How would you change the background colour of a div with the id "colourme" to purple?


How are objects created in arrays


What is the difference between global and local variables?


We're here to help

contact us iconContact ustelephone icon+44 (0) 203 773 6020
Facebook logoInstagram logoLinkedIn logo

© MyTutorWeb Ltd 2013–2025

Terms & Conditions|Privacy Policy
Cookie Preferences