In this article we will show you the solution of jQuery set innerHTML, in jquery we can’t use innerHTML because they provided another methods of text() and html() replaced this. So here we are going to use html() method for achieve the result.
The html() method also having same behavior of innerHTML so we can append any string or text on any element in html document.
Step By Step Guide On jQuery Set innerHTML :-
In html we defined p element with id attribute and button ‘Set’ with id ‘st’.
In script block we defined ready() method which is automatically starts loading code of within this function when this program opened on browser.
There we appended click() method on button and within this we specified p tag then which is appends with html() method for sets some text on paragraph tag.
<!DOCTYPE html> <html> <head> <title>Set innnerHTML</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script> <script> $(document).ready(function(){ $("#st").click(function(){ $("#pele").html("Hi Everyone"); }) }); </script> <body> <p id="pele"></p> <button id="st">Set</button> </body> </head> </html>
- <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
- The<html> tag is used to indicate the beginning of HTML document.
- As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
- Here we imported open source jquery library support file which is needed for coding using jquery in program.
- Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
- <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
- We defined paragraph element with id ‘pele’ for inserts text on it and button ‘Set’ with id ‘st’ for proceeds process when user clicks on it.
- In script we defined ready() method within that we appended click() method on button ‘Set’ by id attribute ‘st’. There we specified p element by id attribute ‘pele’ and which is appends with html() method with string ‘Hi Everyone’.
- Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.
Conclusion :-
In conclusion now we are able to know how to set text on element instead of innerHTML in jquery.
Before execution of program we needs to confirm internet connection because then only that jquery file will supports defined jquery codes without error.
When we executes program on browser we can see button ‘Set’ now user needs to clicks on it then we can see added text ‘Hi Everyone’ on paragraph element.
If you wants to add any other text or string just modify it accordingly and you can also use text() method instead of html().
I hope this article on jQuery set innerHTML helps you and the steps and method mentioned above are easy to follow and implement.