i've few dynamic elements being generated on click of button. on click of button, dynamic divs generated contain link.
i first href attribute value("link0.com") in array "linkarr".
how of href attributes stored inside array?
here's code.
<!doctype html> <html> <head> <meta charset="utf-8"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"> <style> .parent { height: 25%; width: 90%; padding: 1%; margin-left: 1%; margin-top: 1%; border: 1px solid black; } .parent:nth-child(odd){ background: skyblue; } .parent:nth-child(even){ background: green; } </style> </head> <body> <button class="btn btn-primary" onclick="getdata()">get data</button> <div class="box"> </div> <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script> var content = ""; linkarr = new array(); function getdata(){ var count = 0; for(count=0; count<5; count++) { content+= '<div class="container-fluid parent"><div class="row"><div class="col-md-6">number: '+count+'</div><div class="col-md-6"><a href="link'+count+'.com" class="mylink">link'+count+'</a></div></div></div>'; } $('.box').html(content); $('.box').each(function(){ linkarr.push($(this).find('.mylink').attr('href')); console.log(linkarr); }); } </script> </body> </html>
since each of link elements has it's own class, why not .mylink
elements selector , push href
attribute of each of them linkarr
array?
var content = "", linkarr = []; function getdata() { var count = 0; (count = 0; count < 5; count++) { content += '<div class="container-fluid parent"><div class="row"><div class="col-md-6">number: ' + count + '</div><div class="col-md-6"><a href="link' + count + '.com" class="mylink">link' + count + '</a></div></div></div>'; } $('.box').html(content); $('.mylink').each(function() { linkarr.push($(this).attr('href')); }); console.log(linkarr); }
.parent { height: 25%; width: 90%; padding: 1%; margin-left: 1%; margin-top: 1%; border: 1px solid black; } .parent:nth-child(odd) { background: skyblue; } .parent:nth-child(even) { background: green; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="btn btn-primary" onclick="getdata()">get data</button> <div class="box"></div>
Comments
Post a Comment