Initializing
Liveweave
Web
expand_more
home
Home
data_object
CSS Explorer
arrow_outward
Palette
Color Explorer
arrow_outward
Polyline
Graphics Editor
arrow_outward
outbox_alt
Generative AI
arrow_outward
frame_source
Python Playground
New
arrow_outward
build
Tools
expand_more
restart_alt
Load "Hello Weaver!"
post_add
Generate Lorem ipsum...
code
Format HTML
code_blocks
Format CSS
data_object
Format JavaScript
library_add
Library
expand_more
A
Algolia JS
Animate CSS
Apex Charts JS
B
Bulma CSS
Bootstrap
C
Chart JS
Chartist
Create JS
D
D3
Dojo
F
Foundation
Fullpage JS
G
Granim JS
Google Charts
H
Halfmoon
J
jQuery
M
Materialize
Moment JS
Masonry JS
Milligram CSS
P
Pure CSS
Primer CSS
Popper JS
Pattern CSS
Picnic CSS
R
React JS
Raphael JS
Raisin CSS
S
Semantic UI
Skeleton CSS
Spectre CSS
Tachyons CSS
T
Tailwind
Three JS
U
UI Kit
Vis JS
W
Water CSS
download
Download
expand_more
developer_mode
Download as HTML
folder_zip
Download as .ZIP
cloud_upload
Save
account_circle
Login
settings
Settings
expand_more
14
px
Live mode
Night mode
Line number
Mini map
Word wrap
sync_alt
Reset Settings
smart_display
Run
<!DOCTYPE html> <html> <head> <title>Detect Element Children (Numeric)</title> <meta charset='utf-8'> <meta name='viewport' content='initial-scale=1.0'> <script type='text/javascript' src='http://code.jquery.com/jquery-latest.min.js'></script> </head> <body> <div class="contenu" align="center"> <div> Value number = .enfants children </div> <input type="number" min="0" class="ajouter-enfants" value="0" /> <div class="enfants"> </div> </div> </body> </html>
body { color: #fff; font: 16px arial; background-color: #222; } .contenu > div:nth-child(1) { font-size: 12px; padding-top: 10px; } .enfants > div { width: 80%; background-color: #4a4; padding: 1em 0; } .ajouter-enfants { color: #fff; font: 16px arial; background-color: #0aa; margin: 1em; padding: 1em 2em; border: 0; border-radius: 1em; } .ajouter-enfants:focus { background-color: #0cc; }
$(document).ready(function() { var counter = 1; // Value number = .enfants children $(".ajouter-enfants").on('keyup change', function() { var yourChildren = "<div>" + counter++ + "</div>"; var CallAppend = function() { $(".enfants").append( yourChildren ); }; // If 0 or empty clear container if ( $.inArray($(this).val(), ["0", "", " "]) > -1 ) { $(".enfants").html(""); // If only add/have 1 div in container } else if ($(this).val() === "1") { $(".enfants").html("").append( yourChildren ); // If > 0 add as many divs as value says } else { $(".enfants").html(""); CallAppend(); } }); });