I am getting two problems in my jquery code. I could not understand the cause for it.
1.I am calling f1() twice- one inside the 'submit' eventhandler and second one, outside the handler. If I remove the f1() call that lies outside the handler, I could see that event handler doesn't get triggered when I click the submit button. (ie. no alert popups)
2.I am always seeing that my ctypes.length is 0 when ever the alert popups. I thought, it has to be the number of fields in the form which is 4
<!DOCTYPE html>
<html>
<head>
<link href="http://nvd3.org/assets/css/nv.d3.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://nvd3.org/assets/js/nv.d3.js"></script>
<script>
function f1(){
ctypes = $("form.chart-series input:checkbox");
alert(ctypes.length);
}
f1();
$('.chart-series').submit(function(event){
event.preventDefault();
f1()
});
</script>
</head>
<body>
<div>
<form class="chart-series">
<ul>
<li><input type="checkbox" name="chart_name" value="1" checked/> a</li>
<li><input type="checkbox" name="chart_name" value="2" checked/> b</li>
<li><input type="checkbox" name="chart_name" value="3" /> c</li>
<li><input type="checkbox" name="chart_name" value="4" /> d</li>
<input type="submit" class="btn chart-submit" value="update"/>
</ul>
</form>
</div>
</body></html>