0

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>
2
  • Your script code does get executed before the form element even exists – so of course you get a length of 0, because no elements were matched, and you also did not attach a submit handler to anything. Commented Aug 13, 2014 at 23:37
  • As CBroe said run your code after DOM is ready($(document).ready(function{}())) Commented Aug 13, 2014 at 23:38

2 Answers 2

1

Insert scripts at bottom of the page and wrap your code in $(document).ready();

<!DOCTYPE html>
<html>
<head>
<link href="http://nvd3.org/assets/css/nv.d3.css" rel="stylesheet">
</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>
<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>
$(document).ready(function () {
function f1(){
ctypes = $("form.chart-series input:checkbox");
alert(ctypes.length);
}

f1();
$('.chart-series').submit(function(event){
    event.preventDefault();
    f1()
});
});
</script>
</body></html>
Sign up to request clarification or add additional context in comments.

Comments

1

Wrap it in $(document).ready()

$( document ).ready(function() {
    function f1(){
        ctypes = $("form.chart-series input:checkbox");
        alert(ctypes.length);
    }

    f1();
    $('.chart-series').submit(function(event){
        event.preventDefault();
        f1();
    });
}); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.