Solved

JQuery Error and run twice

Posted on 2013-11-02
3
193 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

777 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