append method only inserted the element once
Hello everyone:
I am doing an exercise to insert an html element after the selected elements.
1. I noticed that using append() method will let me insert the html element right after the matching elements
2. using insertAfter method will insert the html element directly below the mathching elements
However when I used the id element in the append method, it only insert the html element once, instead of inserting it for all the matching elements. Please review my code and advise me why it happened and what should I do to insert the element 3 times by using append(). Thanks!
P.S. i want to insert the button element 3 times for each paragraph with id value 'disclaimer' by using append method.
- <html>
- <head>
- <title>hello world</title>
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <link rel="stylesheet" href="stylesheet1.css" type="text/css" />
-
- <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" ></script>
- <script language="JavaScript" type="text/javascript">
- <!--
- $(document).ready(function(){
- //declare a variable to hold input element button
- //can't append button to each paragraph
- var test = $('<input type="button" id="toggleButton" value="hide" />');
-
- $('#disclaimer').append(test);
- //$('p').append(test);
- //$(test).insertAfter('p');
- });
-
-
- //-->
- </script>
- </head>
- <body>
-
- <p id="disclaimer">this is the first paragraph</p>
- <p id="disclaimer">this is the second paragraph</p>
- <p id="disclaimer">this is the third paragraph</p>
-
- </body>
- </html>