Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
$ not defined using jQuery in Wordpress

Ok, so I'm having some trouble here. I'm customizing a plugin. I need a text field to automatically populate with the text of a drop down menu when an option is selected. The ID's of both elements are created on age load via php; however the IDs do not change. The id for the is "field_new_post_category" and the id of the text field is "post_title" This is what I have so far:

<script type='text/javascript'>//<![CDATA[ 
$(function(){
$(document).ready(function(){
    $("#field_new_post_category").change(function(){
        $("#post_title").val($(this).find("option:selected").text());
    });
});
});//]]>  
</script>
<select name="field_new_post_category" id="field_new_post_category" class="categoriesBox">
<option class="level-0" value="1">Main Category</option>
<option class="level-1" value="5">&nbsp;&nbsp;&nbsp;sub-category</option>
<option class="level-2" value="7">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sub-sub-category</option>
</select>

<input type="text" id="post_title" class="span5" name="post_title" value="Campaign Title" readonly="readonly"/>

I have this working well in a JSFiddle here: http://jsfiddle.net/XVDH2/39/

The trouble is that when I put it all together in Wordpress, it doesn't work. Simply nothing. I make the selection and nothing changes.

Thanks for any help you might be able to offer. :)

-Chase

share|improve this question
Have you checked the console if there are any javascript errors? your script may not be executing at all. Try, pasting your script in console. – M-R Dec 11 '12 at 10:00
the only error its showing is this: Error: TypeError: $ is not a function – Michael Chase Dec 11 '12 at 10:08
This is not "only" an error. It is the error. Also, admin.php does not exist. – brasofilo Dec 11 '12 at 11:07
Thanks for the help guys. – Michael Chase Dec 11 '12 at 21:39

marked as duplicate by brasofilo, toscho Dec 11 '12 at 11:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 0 down vote accepted

"TypeError: $ is not a function "

error shows up when either jQuery is not loaded or the jQuery object is not assigned to $. Possible solutions are

  1. Try including jQuery wp_enqueue_script('jquery'). (you can check its inclusion by typing 'jQuery' in console and pressing Enter)
  2. If it is already loaded. use "jQuery" instead of "$"

So, your code will look like this

jQuery(function(){
jQuery(document).ready(function(){
    jQuery("#field_new_post_category").change(function(){
        jQuery("#post_title").val(jQuery(this).find("option:selected").text());
    });
});
});

hope, it works

share|improve this answer
I tried both options, when I add the wp_enqeue_script() it gives me the same error as before. if I use jquery instead of '$', it says: ReferenceError: jquery is not defined – Michael Chase Dec 11 '12 at 10:25
if I do both, it still gives me that second error – Michael Chase Dec 11 '12 at 10:26
1  
make sure you tried with capital Q.. jQuery not jquery. – M-R Dec 11 '12 at 10:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.