Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the HTML and JavaScript that I'm using for this.
The HTML is a table with 2 numbers and a input area. The user is supposed to

Here is my JavaScript. My problem is that I need to use jQuery. Everything I try doesn't work.

What I have tried:

<pre><pre lang="HTML">
<pre>	
<pre><!Doctype html>
<html>

<head>
<link rel="stylesheet" href="assignment-Jasmine.css">
<title>JavaScript to JQuery</title>
<script
  src="https://code.jquery.com/jquery-3.5.1.min.js"
  integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
  crossorigin="anonymous">
</script>
<style>
		td{
			padding-top: 5px;
			padding-bottom: 5px;
			padding-right: 20px;
			padding-left: 20px;
		}
		.wrong {
			background-color: #fa8072;
		}
 
		.right {
    		background-color: transparent;
 		}
</style>
</head>

<body>

Math!


Try to see if you can do the math



5 3
7 6
5 5
8 3
4 7
3 9
8 5
2 6
5 9
6 6

</body>
</html>





<pre lang="Javascript">

<pre>		(function( math )  { 
				   'use strict';
				   var tr = math.querySelectorAll( 'tr' ), 
					   td = math.querySelectorAll( 'td' ), 
					  inp = math.querySelectorAll( 'input' ),
				   totals = [], c;
			   for ( c = 0; c < tr.length; c ++ ) {
					 totals.push( parseFloat( td[ c * 3 ].textContent ) + 
								  parseFloat( td[ c * 3 + 1 ].textContent ) );
					 inp[ c ].value = ' ';				   
					 inp[ c ].addEventListener( 'blur', check_answer( c ), false );

				}

		function check_answer( c ) {
			   inp[ c ].onblur = function() { 
				  if( inp[ c ].value == totals[ c ] ) {
					  tr[ c ].classList.add( 'right' );
					 }
						  else {
				 tr[ c ].classList.remove( 'right' );
				 tr[ c ].classList.add( 'wrong' );
							 }
						  }			   
					   }
		 }( document ) ); 

$( window ).unload(function(math) {
});
Posted
Comments
F-ES Sitecore 8-May-20 11:33am    
querySelectorAll is just $, so $("tr") and addEventListener is "on". You could even replace the loops with "each". Look at the jQuery docs, they're quite extensive with examples.
Member 14825986 8-May-20 11:42am    
Is this even close

$(totals) = [], c;
$('tr','td','input'){
each(( c = 0; c < tr.length; c ++ ) {
totals.push( parseFloat( td[ c * 3 ].textContent ) +
parseFloat( td[ c * 3 + 1 ].textContent ) );
inp[ c ].value = ' '; } )
onblur('blur', check_answer( c ), false )
};


$(check_answer(c)){
onblur( if( inp[ c ].value == totals[ c ] ) {
tr[ c ].classList.add( 'right' );
}
else {
tr[ c ].classList.remove( 'right' );
tr[ c ].classList.add( 'wrong' );
})
};


$( window ).unload(function(math) {
});
ZurdoDev 8-May-20 11:51am    
Why do you need to use jquery? jQuery IS javascript. I do like it but you don't have to use it.
Member 14825986 8-May-20 11:53am    
Im being asked to change it over.
Maciej Los 14-May-20 3:49am    
Use "Reply" widget to be sure that system will inform member about your reply.
You'll find that widget on the right side of member's nick/login.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900