Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  195] [ 4]  / answers: 1 / hits: 54123  / 10 Years ago, mon, april 28, 2014, 12:00:00

with this piece of jQuery I check if some fields are match or are not empty, but I get this error.



Uncaught TypeError: Cannot read property 'match' of undefined



Can anyone tell me what I am doing wrong here?



if ( width.match( /^d+$/ ) && height.match( /^d+$/ ) && type.length > 0 && color.length > 0 ) {


This is the full code:



if( $( #config ) ) {
$( 'input, select' ).on( 'change', function(){
var width = $( #config-steps #width ).val();
var height = $( #config-steps #height ).val();
var type = $( #config-steps #type ).val();
var color = $( #config-steps #selected-color ).val();

if ( width.match( /^d+$/ ) && height.match( /^d+$/ ) && type.length > 0 && color.length > 0 ) {
$( #checkout-message ).show();

// Change visible price
$( #change-price ).html( calculate_price().toFixed( 2 ) );

} else {
return false;
}

});
}

More From » jquery

 Answers
55
if( $( #config ) ) {


is always going to be true with jQuery since jQuery returns an object and objects are truthy. The check should be checking the length which will return a number and zero is false.



if ($(#config).length) {


Now when jQuery does not find an element, than val() will return undefined, so it is not finding the element.


[#71266] Saturday, April 26, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alorac

Total Points: 262
Total Questions: 82
Total Answers: 97

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
alorac questions
Sat, Oct 10, 20, 00:00, 4 Years ago
Tue, Sep 22, 20, 00:00, 4 Years ago
Wed, Jul 1, 20, 00:00, 4 Years ago
Wed, Jun 3, 20, 00:00, 4 Years ago
Sun, May 17, 20, 00:00, 4 Years ago
;