Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

onurtekin
7,346 PointsUncaught TypeError: undefined is not a function (WP Admin)
Hi,
i'm writing a plugin for wordpress. It creates a new post type. I have a js file which is embedded in with wp_enqueue_script method.
I'm trying to read the post_title text input, count the words. I'm stuck in this error:
Uncaught TypeError: undefined is not a function get_word_count (anonymous function)
I have nothing but the following, in my js file:
//Getting the number of words in 'title' field to send as a custom_field.
function get_word_count(){
a = document.getElementByName("post_title").value;
document.getElementById("kelime_sayisiw").value = a.split(" ").length;
};
document.getElementsByName("post_title").onkeyup = get_word_count();
any suggestions on getting rid of this error?
4 Answers

Aaron Graham
18,033 PointsThe type error is probably coming from this line:
a = document.getElementByName("post_title").value;
Element should be plural: .getElementsByName()

onurtekin
7,346 Pointsoh, that tiny letter. Thank you :)
here i have another one:
function get_word_count(){
window.alert (document.getElementById("title").value);
};
window.alert (document.getElementById("title").value);
get_word_count, triggered by a button, gives me the correct alert, , but the last line tells me that it can not read the 'title'. "Cannot read property 'value' of null"
Am i missing a tiny thing again?

Aaron Graham
18,033 PointsI am going to guess that you are loading this script before the DOM is loaded. If you are loading your script in <head>
or at the top of <body>
, this can happen. The easiest thing to try would be to just move your <script>
to just before the closing </body>
. See if that works.

onurtekin
7,346 PointsThank you again Aaron.
I have tried different things before your reply and managed it without changing the place of the script call.
//Getting the number of words in 'title' field to send as a custom_field.
function get_word_count(){
b = document.getElementById("title").value;
document.getElementById("kelime_sayisiw").value = b.split(" ").length;
};
window.onload = function() {
get_word_count();
var el = document.getElementById("title");
el.addEventListener("keyup", get_word_count, false);
};

Aaron Graham
18,033 PointsI thought about mentioning window.onload
, but it's my understanding that it might not be well supported among older browsers. Another option, and one that provides some backward compatability, is to use jQuery and the $(document).ready()
function. It does basically the same thing as window.onload
but has better support for old browsers. If supporting older browsers isn't something you care about, just forget I said any of this... Anyway, glad you got it working!

onurtekin
7,346 PointsSince this is a wp-admin page, and it records an important dat, wider browser support is important. I don't know about Jquery yet but i'll consider it definetly. Thank you.