<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<title>Javascript Create Div Element Dynamically</title>
<style type="text/css">
.ex {
width:200px;
position: relative;
background-color :#CCC;
height:150px;
padding:10px;
margin:5px;
left-margin:0px;
float :left;
}
#newdiv {
width:800px;
height:800px;
border:1px solid #000;
}
.border {
border:1px solid #000;
}
</style>
<script>
image.cc = 1;
function changeimage(image) {
if (image.cc == 0) {
image.cc = 1;
$(image).attr('src', 'images/white_contact.png');
} else if (image.cc == 1) {
image.cc = 2;
$(image).attr('src', 'images/yellow_contact.png');
} else if (image.cc == 2) {
image.cc = 3;
$(image).attr('src', 'images/green_contact.png');
} else {
image.cc = 0;
$(image).attr('src', 'images/red_contact.png');
}
}
</script>
<script type="text/javascript" language="javascript">
var i = 0;
function createDiv() {
if (i < 6) {
$('#newdiv').append('<div id="div' + i + '" class="ex" style="text-align: left;"><img class="myimage" onclick="changeimage(this)" border="0" src="images/white_contact.png" width="60"/></div>');
$('#newdiv').find('#div' + i).append('<table border="0"><tr>' +
'<td>Name:</td>' +
'<td><input type="text"></td>' +
'</tr><tr>' +
'<td>Title:</td>' +
'<td><input type="text"></td>' +
'</tr>' +
'<tr>' +
'<td>Contact:</td>' +
'<td><input type="text"></td>' +
'</tr>' +
'</table>');
}
i++;
$(".ex").draggable({
containment: 'parent',
cursor: 'pointer',
opacity: 0.6
});
$(".ex").droppable();
}
</script>
</head>
<body>
<p align="left"> <b>Click this button to create div element dynamically:</b>
<input id="btn1" type="button" value="create div" onClick="createDiv();" />
<div id="newdiv"></div>
</p>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |