Bu yazımızda jQuery ile Dizi kullanımı ile ilgili bir örnek oluşturacağız. Örneğimizde bir dizi oluşturarak “EKLE” butonu ile text kutusuna girilen değerin diziye eklenmesini ve “LİSTELE” butonuna basıldığında listelenmesini sağlayacağız. Dizi içindeki değerleri listelemek için For Döngüsünden faydalanacağız.
.html sayfasına ait kodlar:
Ekran Çıktısı:
.html sayfasına ait kodlar:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>www.yazilimkodlama.com</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <style> #sonuc{ color:crimson; margin-top: 20px; } </style> </head> <body> <h2>jQuery Dersleri </h2> <input type="text" id="txtad"> <input type="button" value="EKLE" id="btn1"> <input type="button" value="LİSTELE" id="btn2"> <div id="sonuc"></div> <script> $(document).ready(function(){ var isimler=[]; $("#btn1").click(function(){ isimler.push($("#txtad").val()); }); $("#btn2").click(function(){ for(var i=0;i<isimler.length;i++) { $("#sonuc").append(isimler+" "); } }); }); </script> </body> </html> |
Ekran Çıktısı:
