I am new to JavaScript and here is a challenge I don't know how to overcome!
Here is the sample code I tried:
<body>
<p id="hello">Hello world.</p>
<script type="text/javascript">
var store,newString,finalString;
//i want to store the content of <p> tag in store variable
store=?
newString="I am Shuvrow";
finalString = store + newString;
//finally i want to replace the content of <p> tag by finalString variable
</script>
</body>
Use the innerHTML
attribute of the document element, as such:
<body>
<p id="hello">Hello world.</p>
<script type="text/javascript">
var store,newString,finalString;
// Get the contents of the element with the ID of 'hello' and assign it to the store variable.
store = document.getElementById('hello').innerHTML;
newString="I am Shuvrow";
finalString = store + newString;
// Replace the contents of the element with the ID of 'hello' with the value of the finalString variable.
document.getElementById('hello').innerHTML = finalString;
</script>
</body>