0

Okay I have this problem. I am using this variable in one function, but I have to reset it in another, but I'm not sure how to accomplish it.

My code looks like this:

var city_flag = null;
  $(".button").click(function(){
    var city = $(this).attr("id");

    if(city_flag == city){
      city_flag = null;
    } else {
      city_flag = city;
    }
  });

And than I got this: (coffescript)

$.fn.extend
  somelistMap: (options) ->
    // some settings here

   // on init addCloseClickListenerToInfoWindow called

  addCloseClickListenerToInfoWindow = (id) =>
      google.maps.event.addListener markers[id]["infoWindow"], 'closeclick', =>
        resetMap()
        $(".city_tab").css("background", "none")
        $(".all_dealers").removeClass("row_hide")
        $(".all_dealers").addClass("row_show")
        city_flag = null //Here i have to reset it

How can I get this to work? I tried putting city flag outside of the functions, but its not working!

EDIT: Forgot to mention environment is rails.. if that changes something

5
  • declare that variable as GLOBAL Commented Mar 25, 2014 at 12:35
  • put var city_flag outside your document.ready Commented Mar 25, 2014 at 12:36
  • ok i will try that....sec Commented Mar 25, 2014 at 12:37
  • flag or city_flag you seem to be interchanging them Commented Mar 25, 2014 at 12:37
  • sorry my bad, in code this was right ;) Commented Mar 25, 2014 at 12:43

4 Answers 4

2

declaring global variables is not a best practice but it will solve this specific problem.

use this whenever you access city_flag: window.city_flag.

for example:

window.city_flag = null;

or

window.city_flag = city;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried this already too. nothing seems to change! 'city_flag' does not reset!
1

Make city_flag as a GLOBAL var like,

var flag = null,
    city_flag = null;
// ..... your remaining code

Also you can use this.id instead of $(this).attr("id") like,

var city = this.id;

Comments

0

To get this work do the below changes (I dont know your Full coffeescript structure , this is just a assumption)

$.fn.extend
  somelistMap: (options) ->
    // some settings here

   // on init addCloseClickListenerToInfoWindow called
//////////////////
  var that = this;
///////////////
  addCloseClickListenerToInfoWindow = (id) =>
      google.maps.event.addListener markers[id]["infoWindow"], 'closeclick', =>
        resetMap()
        $(".city_tab").css("background", "none")
        $(".all_dealers").removeClass("row_hide")
        $(".all_dealers").addClass("row_show")
        that.city_flag = null //Here i have to reset it

Comments

0

Okey...i tried everything mentioned, nothing seemed to work :/

So i came up with this solution, i created hidden element and just took my value with ("#hidden").html() and to reset it ("#hidden").html("null")

I know it is not the best solution, but this workaround worked!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.