Solved

JQuery Error and run twice

Posted on 2013-11-02
3
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…
Suggested Courses

630 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