How to get a value from the anchor tag and use it for another function

advertisements

I have a very simple problem but I can't figure it out, Im newbie with regards to this.

I have a following functions where user will click a button of something he/she wants to search, then when he/she chose and clicks something around the choices,he/she will click a search button to search.

Here is my code for getting the value from anchor tags, the choices is wrapped up in anchor tags.

$(document).on('click' , '#area_id_1' , function(){
var getText1 = $("#area_id_1").text();
alert(getText1); return false; });

$(document).on('click' , '#area_id_2' , function(){
var getText1 = $("#area_id_2").text();
alert(getText1);
return false;
});

$(document).on('click' , '#area_id_3' , function(){
var getText1 = $("#area_id_3").text();
alert(getText1); return false;});

$(document).on('click' , '#area_id_4' , function(){
var getText1 = $("#area_id_4").text();
alert(getText1);
return false;
});

$(document).on('click' , '#area_id_5' , function(){
var getText1 = $("#area_id_5").text();
alert(getText1); return false;});

$(document).on('click' , '#area_id_6' , function(){
var getText1 = $("#area_id_6").text();
alert(getText1); return false;  });

$(document).on('click' , '#area_id_7' , function(){
var getText1 = $("#area_id_7").text();
alert(getText1); return false;  });

$(document).on('click' , '#area_id_8' , function(){
var getText1 = $("#area_id_8").text();
alert(getText1); return false; });

and here is my function for search button //button for search function

$(document).on('click', '#btnSearch', function() {

}

The question is, how will I be able to store the value that I got from the anchor tags so I can use it when I click the btnSearch function?


First of all define your variable getText1 global as below.

var getText1;

$(document).on('click' , '#area_id_1' , function(){
    getText1 = $(this).text();
    return false;
});

...(rest of your code)
// and you will get the variable in below function

$(document).on('click', '#btnSearch', function() {
    alert(getText1); // you can check here if it is empty or not
}