Solved

JQuery Error and run twice

Posted on 2013-11-02
3
191 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
ID: 39618670
.Ctitle is dynamically added to the DOM in the flay.
0
 
LVL 44

Expert Comment

by:Rainer Jeschor
ID: 39618691
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
ID: 39618705
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

914 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

20 Experts available now in Live!

Get 1:1 Help Now