Solved

JQuery Error and run twice

Posted on 2013-11-02
3
188 Views
Last Modified: 2013-11-06
I have this code:
<script type="text/javascript">
    var names_count = 0;
    var c_count = 0;
    var blocked_seats = 0;
    names_count = 0;
    c_count = 0;
    blocked_seats = 0;



    $(function () {


        $('input#SaveBTN.link').click(function (e) {

			

            e.preventDefault();
			alert("HERE");
            $('.Ctitle').each(function () {
                if ($(this).val() != "Infant") {
                    c_count = c_count + 1;
                    //				

                }


            });
            //alert('namecount '+c_count);


            names_count = c_count + 1;
            blocked_seats = parseInt($('.seats').val());
            //alert(blocked_seats);
            $('.seats').val(blocked_seats - names_count);
            // $("#New_booking_form").submit();
            //alert(names_count);
            names_count = 0;
            c_count = 0;
            blocked_seats = 0;
        });




    });
    names_count = 0;
    c_count = 0;
    blocked_seats = 0;
</script>

Open in new window

I don't know whats wrong!
0
Comment
Question by:dorarishome
3 Comments
 

Author Comment

by:dorarishome
Comment Utility
.Ctitle is dynamically added to the DOM in the flay.
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
Comment Utility
Hi,
could you please provide either a link to your site or add the html?
What error do you get?
It is impossible to debug this without the related HTML source.
Thanks.

KR
Rainer
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
Comment Utility
Hi mate... I'm missing a lot of information here.

What is this supposed to do?
What's the error?

I've created a demo here: http://jsfiddle.net/bkL2L/ but still I'm missing elements to test this properly.
Can you improve this test environment to replicate your error?

From your code I see some potential breaking points:
blocked_seats = parseInt($('.seats').val());

Open in new window

If $('.seats') returns more than one item, parseInt will break.

Another strange thing I see is that you're resetting the value of the same variables at the beginning and at the end of the code... Why? It makes no sense. What are you trying to accomplish?

Be aware that you're declaring those variable in the global scope which will make them (as the name says) global to all javascript code, independently if it's in-line in the page or in any other file.

Javascript looks like a fairly simple language but it's actual a functional language, something that most devs never touched and when is not well understood it will lead to a lot of unexpected results. It's fundamental to know how it actually works.
I strongly advise you to read at least one of these books:
javascript the good parts
javascript patterns
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now