SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: error on page

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error on page

    Hello all,

    Im hoping i can get some help with what may be a simple fix.

    Im using jquery to validate an image upload, and its working a treat.

    However, the js file that the coding is on is called in the header throughout the site.

    When the code isnt used (ie the page has no upload form) it throws an error in IE saying...

    object doesn t support this property or method jquery

    I assume its because it cant find the right form to run the code on?

    Any ideas?

    I tried firebug, and it wont load the page, it just hangs and highlights this line....

    Code:
    $().ready( function() {
    31 $("#addImageForm").validate({
    32 event: "keyup",
    33 rules: {
    34 image_title: { required: true },
    35 image_filename:{ required: true,
    36 accept: "(jpe?g|gif|png|GIF|JPG|PNG)"
    37 }
    38
    39 },
    40 messages: {
    41 image_title: "The Image needs a name",
    42 image_filename: {
    43 required: "You need to select an image to upload",
    44 accept:"Only image files are allowed. (gif, jpg or png)"
    45 }
    46 }
    47 });
    48});
    (line 44)

  2. #2
    SitePoint Guru Dashman's Avatar
    Join Date
    Jan 2006
    Location
    Manchester, UK
    Posts
    627
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Im not an expert on jQuery (or JS for that matter), but, somewhere in your script, where you detect the form, add the following lines:

    HTML Code:
    if(!document.getElementById('addImageForm')){
    return;
    }
    else{
    var theFormTovalidate = document.getElementById('formIdnameHere');
    ***
    }
    Hope that is useful in some way ...

    D

  3. #3
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's looking for something with the ID #addImageForm and if it doesn't find it it's going to throw an error. You have to add something to check if it's there:
    Code Javascript:
    if $('#addImageForm') {
      $("#addImageForm").validate({
        ...
      });
    }

  4. #4
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies.

    I tried the suggestions and the jquery one didnt work, but i think that is because it is trying to test the boolean value of an object - which doesn't have one .. so i tried this...
    Code:
     if($("#addImageForm").size > 0){
    ...
    .
    ..
    ..
    ..
    }
    And this stops the error, but it wont execute the code. inside even when i think it should be true.

    We are getting closer though!!

    any more ideas?
    Last edited by billybrag; Jun 20, 2007 at 05:23. Reason: code tags!

  5. #5
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just use document.getElementById then. I don't see why the jquery syntax shouldn't work, what is it returning? If it's a reference to an HTML element, it should not be evaluating to false.

    Doing if(document.getElementById) is just fine, as is if(var) where var may be an integer, object literal, array, string... basically, in if statments (and other things like while) the contents are treated as true if they aren't false, undefined, 0, an empty string or null.

    Try an alert on $('#addImageForm'), see what it returns.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks again for the reply,

    Here is the full code of what i am using....

    for the js I have...

    Code:
    if($("#addImageForm").size() > 0){
    $().ready( function() {
    
    	$("#addImageForm").validate({
    		event: "keyup",
    		rules: {
    			image_title: { required: true },
    			image_filename:{ 	required: true,
    								accept: "(jpe?g|gif|png|GIF|JPG|PNG)"
    			}
    			
    		},
    		messages: {
    			image_title: "The Image needs a name",
    			image_filename: {
    						required: "You need to select an image to upload",
    						accept:"Only image files are allowed. (gif, jpg or png)"
    							}
    		}
    	});
    });
    and the html code is

    Code:
    
    	<form method="post" class="addform" id="addImageForm" enctype="multipart/form-data" >
    	 <input type="hidden" name="process_form" />
    		<table>
    			<tr>
    				<td>Image Name</td>
    				<td><input type="text" id="image_title" name="image_title" /></td>
    			</tr>
    			<tr>
    				<td colspan="2">Add a short description for the Picture below</td>
    			</tr>
    			<tr>
    				<td colspan="2"><textarea id="image_description" name="image_description" cols="45" rows="50"  ></textarea></td>
    			</tr>
    			<tr>
    				<td>Image Filename</td>
    				<td><input  class="{accept:true}" type="file" id="image_filename" name="image_filename" /></td>
    			</tr>
    			
    			<tr>
    				<td><input type="submit" name="submit" value="Add Image" /></td>
    				<td><input type="reset" name="reset" value="Reset" /></td>
    			</tr>
    		</table>
    	</form>
    So basically I want to do the error checking when the above form exists.

    At the moment it does not execute the code whether the form is on that page or not - but it doesnt error either.

    I placed an alert in the js if statement and it doesnt pop up ever?

    :s

  7. #7
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can anyone help with this?

  8. #8
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The alert isn't working because the if statement is resolving to false. Put the alert outside the if statement to see what $('#addImageForm') returns.

    The problem is with this:
    Code:
    if($("#addImageForm").size() > 0) { }
    What is size() ? It probably is something that can't deal with what $() returns. I can see no reason for $("#addImageForm") not working unless it is returning null, undefined, 0, false or an empty string. In which case the form doesn't exist or there is something wrong with that function.

    In any case, with a nodeList, you should use the length property, not this custom size() thing.

  9. #9
    SitePoint Addict
    Join Date
    Aug 2004
    Location
    Swindon
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again Raffles!!

    I think i may have caught it - the if statement was outside of the $().ready( function() where as i think it should have been the other way around!!

    idiocy on my part

    I used the length attribute as you mention too - I got the size one from visual Jquery, where it appeared to be similar!

    I think* though that it is sorted


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
  •