ThemeShaper Forums » Thematic

Why isn't my Jquery Function working in my child theme?

(2 posts)
  • Started 2 years ago by shelzmike
  • Latest reply from shelzmike
  • This topic is resolved
  1. shelzmike
    Member

    I have a neat little JQuery plugin that adds text hints to the textboxes in forms, by using the title attribute. It works very well - see here: http://remysharp.com/wp-content/uploads/2007/01/input_hint.html

    Now, I successfully added the code to my head that is needed through the functions.php file (which is awesome on a side note because I finally understand how to add filters and whatnot). You can see this by viewing the source of my home page here: http://paradigmforward.com/

    You will notice that my source is exactly the same as listed in the demo example in the first link. I do have the title tag associated with the textbox, but it is not working.

    I have seen that something in WP causes you to have to use slightly different syntax when you use JQuery (very specific, I know!). Is this the case most likely, or is it something else?

    I am not too familiar with JQuery, so if I do need to use different syntax, could someone possibly give me some hints here:

    jQuery.fn.hint = function (blurClass) {
      if (!blurClass) {
        blurClass = 'blur';
      }
    
      return this.each(function () {
        // get jQuery version of 'this'
        var $input = jQuery(this),
    
        // capture the rest of the variable to allow for reuse
          title = $input.attr('title'),
          $form = jQuery(this.form),
          $win = jQuery(window);
    
        function remove() {
          if ($input.val() === title && $input.hasClass(blurClass)) {
            $input.val('').removeClass(blurClass);
          }
        }
    
        // only apply logic if the element has the attribute
        if (title) {
          // on blur, set value to title attr if text is blank
          $input.blur(function () {
            if (this.value === '') {
              $input.val(title).addClass(blurClass);
            }
          }).focus(remove).blur(); // now change all inputs to title
    
          // clear the pre-defined text when form is submitted
          $form.submit(remove);
          $win.unload(remove); // handles Firefox's autocomplete
        }
      });
    };

    which is in the js file, and then

    <script type="text/javascript" charset="utf-8">
    		$(function(){
    			    // find all the input elements with title attributes
    				$('input[title!=""]').hint();
    			});
    		</script>

    Which is how it is called from within my page.

    Thanks!

    Mike

    Posted 2 years ago #
  2. shelzmike
    Member

    Man, you guys are slow :) j/k. Actually, I figured it out. It was easy actually. I mentioned above that I did not get any errors. Not sure how I missed it, but I WAS getting an error. Basically saying something to the effect of "$(function(){...is not a function. Well, it obviously is, but it is because you cannot use the $ shortcut with WordPress and jQuery. Instead, you actually have to write it as follows:

    <script type="text/javascript" charset="utf-8">
    		jQuery(function(){
    			    // find all the input elements with title attributes
    				jQuery('input[title!=""]').hint();
    			});
    		</script>

    Basically, replace the shortcut $ with jQuery. I did this and it worked perfectly fine. Here is a link to a good explanation of they why's if anyone is interested:

    http://techxplorer.com/2008/02/25/using-jquery-with-wordpress/

    Mike

    Posted 2 years ago #

RSS feed for this topic

Reply

You must log in to post.