empty() provides the ability to completely empty the contents of an element.
HTML
- <div class="hello">hello world</div>
empty() will remove the hello world from the html source above
- $('.hello').empty();
If you execute the above function, the hello world of class hello will disappear.
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>quizgum :: jQuery Course</title>
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
- <script type="text/javascript">
- $(function(){
- $('.hello').empty();
- });
- </script>
- <style>
- </style>
- </head>
- <body>
- <div class="hello">hello world</div>
- </body>
- </html>
The above empty() provided only the function of emptying. In other words, the div tag remains and the content disappears?
Remove() removes the div tag from the html source above.
- <div class="hello">hello world</div>
Remove() removes the div tag from the html source above.
- .hello{border:3px solid yellow}
I apply css to show that the tag itself is gone.
- $('.hello').remove();
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>quizgum :: jQuery Course</title>
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
- <script type="text/javascript">
- $(function(){
- $('.hello').remove();
- });
- </script>
- <style>
- .hello{border:3px solid yellow}
- </style>
- </head>
- <body>
- <div class="hello">hello world</div>
- </body>
- </html>
val() provides the ability to change the field value of text.
Input your name in the text box above can be changed with val().
- <input type="text" class="text1" value="quizgum" />
- $('.text1').val('tomodevel');
If you apply jQuery above, value is changed as below.
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>quizgum :: jQuery Course</title>
- <script type="text/javascript" src="https://code.jquery.com/jquery-3.2.0.min.js" ></script>
- <script type="text/javascript">
- $(function(){
- $('.text1').val('tomodevel');
- });
- </script>
- <style>
- </style>
- </head>
- <body>
- <input type="text" class="text1" value="quizgum" />
- </body>
- </html>
After finishing this course, we will learn about focus() and blur().