javascript - how to get data from div tags with same id -


html

<!doctype html> <html>  <body>     <div id="one">         welcome         <div class="two">             hello world         </div>         <div class="two">             bye world         </div>     </div>       <button onclick="myfunction()">      click</button>      <p id="demo"></p>     <script>         function myfunction() {             var x = document.getelementsbyclassname('two')[0].innerhtml;             document.getelementbyid('demo').innerhtml = x;         }     </script> </body>  </html> 

i have 2 divs in div 1 class name ,through function able tha data of class="two" i. hello world.here want data 1 one tha same class name.i want print data class="two" i.e hello world,bye world.plz me

use jquery's each function

we can using jquery's each function. first push every item in array print comma separated.

we push every item in array using push function , use join show it.

<script>   function myfunction(){      msg = array();      $('.classname').each(function(){         msg.push($(this).html());      });       $('#demo').html(msg.join(", "));   } </script> 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -