Good evening! I must say that it is not strong in js - jquery. The following task, there is a pop-up window in which lists of cities and countries are displayed. At the choice of the country, the cities are loaded on ajax, then the person selects the city and goes to the page of this city.

The task is to save the name of the city in localstorage and then display it in the header of the site, that is, just a line with the text that needs to be taken from the link of the city by which we click to go to its page.

At this stage, the code is as follows:

<script type="text/javascript"> $('.dropdown-toggle').click(function(){ var val_input=$(this).text(); localStorage.setItem('keycity',val_input); }); if(localStorage.getItem('keycity')){ $('#loccity').html(localStorage.getItem('keycity')); } </script> 

  • Well done! What is the problem? Or did you just want to share? - Igor
  • It does not work for some reason. Syntax correct? - Bogdan Novik
  • What is '.dropdown-toggle' ? - Igor
  • This is a class of links, there are many of them on the page with such a class - Bogdan Novik
  • The second fragment in other pages too? Is the click processed? Add console.log(val_input); . - Igor

1 answer 1

Delegation:

 $(document).on('click', '.dropdown-toggle', function(){ var val_input=$(this).text(); localStorage.setItem('keycity',val_input); }); 
  • Thank! It helped, I can it further - Bogdan Novik