Calling object method problem

Hi

This is the first time i’m really using javascript objects (well the first time i’ve written my own one) so the problem is probably a very simple one…

I’ve got this:



function city_selector(city, country){

   this.city = get(city);
   this.country = get(country);

  // some other stuff
}

city_selector.prototype.setCities = function (){

   // use some ajax and populate the city drop down box
}

var hometown = new city_selector("hometown","home_country");

<select id="home_country" onchange="hometown.setCities()">
</select>


(very summarised code - let me know if i need to post more)

When i change the country from the dropdown box i get an error “hometown.setCities is not a function” but the thing is that i can call this in other ways eg from the address bar (javascript:hometown.setCities()) and it works perfectly - what am i doing wrong?
As i said in the beginning im new to javascript objects so if you have any pointers it would be much appreciated! :slight_smile:

thanks!

this.city = get(city);
this.country = get(country);

What is get(city) supposed to do?
You don’t show it in your code.

Anyway, get is a reserved word in javascript-
you can’t name a function ‘get’-
so whatever it does, rename it.

oh sorry, it is a function short for document.getElementById - i saw this being used somewhere else so i copied the idea - it seems to work perfectly but i’ll rename it…

That part isnt really important though - the fact is that i can’t seem to call a method from the onchange event of the dropdown menu although i can from other places in the code. Just wondering what im doing wrong…

Post more code.

that code is working for me.

it can be a scope issue. Where are you creating var hometown?

Hi All

Thanks for the responses - i have just found the problem, was a bit stupid of me.
I have a select element with name “hometown” as well and this is where the confusion is coming in - i have renamed the select element and its working perfectly again.

Thanks again though!