Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

JQuery Error and run twice

Posted on 2013-11-02
3
Medium Priority
?
212 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 2000 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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'…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

670 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