SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple jQuery function not working.

    http://cis.jhu.edu/~shanest/project/.../dynamic4.html

    I'm not sure why this is not selecting the input values and writing them anywhere. Can I use the jQuery selectors and functions in a context like this given that they've already been loaded?

    HTML:
    HTML Code:
    <div class="right">
    			<p>
    			Stage 4: 0 <br />
    			Stage 3: <input type="text" id="s3" size="5" /><br/>
    			Stage 2: <input type="text" id="s2" size="5" /><br/>
    			Stage 1: <input type="text" id="s1" size="5" /><br/>
    			<button onclick="p1_calc()">Calculate</button><br />
    
    			<button onclick="p1_check()">Is it correct?</button>
    			</p>
    			<p>
    			Your traverse distance is: <span id="dist"></span> <br />
    			Is this the correct traverse? <span id="correct"></span>
    			</p>
    		</div>
    Javascript (jquery loaded above):
    HTML Code:
    	<script type="text/javascript">
    	<![CDATA[
    		var inputs;
    		var total =  0;
    		function p1_calc() {
    			inputs = $(":text");
    			for(var i = 0; i < 3; i++)
    				total += inputs[i].val();
    			$("#dist").text(total);
    		}
    		function p1_check() {
    			var correct = $("#correct");
    			if(total == 8) 
    				correct.text("You got right!");
    			else
    				correct.text("Sorry, try again...").css("color", "red");
    		}
    	]]>
    	</script>

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FireFox error console says:
    Code:
    Error: syntax error
    Source File: http://cis.jhu.edu/~shanest/project/metricpatterntheory/additional/dynamic/dynamic4.html
    Line: 9, Column: 1
    Source Code:
     <![CDATA[
    So could you please remove <![CDATA[ ]]> and try again.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    They need to be there because this is in an XHTML DOCTYPE document. I've used JS inside those tags before and it works fine in FF and IE.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's saying that my p1_calc() is not defined. This is both with and without the CDATA tags

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see it works now, I've tested it in Opera, FF, IE7.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    35
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea, I just put everything in external js files and loaded them and that worked. Thanks for the help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •