I have some vector layers that I import from geojson files and run some search on them, I want to display only the results of the search as a vector layer.

I am using a jquery form to search, and I get the result but it doesn't show on the map because the page is after that refreshed and the result is lost. I thought of exporting the result in a geojson file this way I can preserve it.

Here is the code for the search it works fine except that the result layer isn't added to the map.

jQuery('#default-search').submit(function(event) {

    var Results=[];
    var src=layer.getSource();
    src.forEachFeature(function(feature) {
    tmp_source=new ol.source.Vector({
                features: Results
    tmp=new ol.layer.Vector({
            title:'temporary layer', 
            source: tmp_source



Is this possible or is there another way easier ?

  • 1
    try to add event.preventDefault(); as first statement in the function, by this you should be able to prevent the reloading of the page.
    – werkuh
    Commented Mar 16, 2016 at 14:50

2 Answers 2


I agree with Hicham (above). Your code seems fine (hard to tell without an example of the results returned) but refreshing of a page on a submit suggests a mishandling of the submit event. (Plenty of Stack Overflow discussion on the topic, here is one I like).

You might try instead attaching an event handler to the submit function that specifically triggers the function where you do something with the results:

$(document).on('keypress', '#default-search', function(event) {
  if(event.which == 13) { //trigger something with the 'enter' key
    //do something

After a long detour to get a solution for this I finally got to the conclusion that we can't write in a file with javascript, and to do so I had to use php with AJAX

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.