<html>
<head>
<script class="jsbin" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<style>
article, aside, figure, footer, header, hgroup,
menu, nav, section { display: block; }
.percent_product { width: 30px; }
</style>
</head>
<body>
<ul>
<li>
<input class="check_product" type="checkbox" checked="checked" name="product_1" />
<input class="percent_product" type="text" value="20" />%
<label class="label_product" for="product_1" data-label="Sky Blue">Sky Blue</label>
</li>
<li>
<input class="check_product" type="checkbox" checked="checked" name="product_2" />
<input class="percent_product" type="text" value="0" />%
<label class="label_product" for="product_2" data-label="Yellow">Yellow</label>
</li>
<li>
<input class="check_product" type="checkbox" checked="checked" name="product_3" />
<input class="percent_product" type="text" value="40" />%
<label class="label_product" for="product_3" data-label="Green">Green</label>
</li>
<li>
<input class="check_product" type="checkbox" checked="checked" name="product_4" />
<input class="percent_product" type="text" value="0" />%
<label class="label_product" for="product_4" data-label="Wood Brown">Wood Brown</label>
</li>
</ul>
<button id="serialize" />Serialize</button>
<div id="debug">
</div>
</body>
</html>
// this function should serialize my form
function mySerialize() {
$('#debug').html( '' );
array_products = new Array();
// for each valid product
$( '.check_product:checked' ).each(
function( i, obj ) {
// index and value
var num = $(obj).next().val();
var label = $(obj).next().next().attr( 'data-label' );
// build array
if( ( num > 0 ) && ( typeof num !== undefined ) ) {
array_products[ label ] = num;
$('#debug').append( 'index: ' + label + ", value: " + num + "<br />" );
}
});
// FROM HERE array_products SEEMS TO BE EMPTY!
// serialize array
var serialized_products = '';
$(array_products).each( function(i, o) {
serialized_products = serialized_products + urlencode(i) + '=' + urlencode(o) + '&';
});
$('#debug').append( 'serialized: ' + serialized_products + "<br />" );
}
// bind event on button click
$(document).ready( function() {
$('#serialize').click( function() { mySerialize(); } );
});
// function helpers
function urlencode (str) {
str = (str + '').toString();
return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').
replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}
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. |