0

In my webapplication, I have the follow pair of function, which open and close a tag , simulating a pop-up windows:

<script>
    function load_page(url){
        $('#results').css("display", "block");
        $('#content').load(url);
    }
    function close(){
        $('#results').css("display", "none");
        $('#content').empty();
    }
    </script>

The first one is working perfectly, but the second don't. I set this function to be called from here:

<div align="right"><button type="button" class="btn btn-lg btn-danger" onclick="close()">Fechar</button></div>

If I replace this code by that other one (without use of the jquery), the method works:

function close() {
      document.getElementById("results").style.display = 'none';
      document.getElementById("content").innerHTML=' ';
  }

Is there any way to do this with jquery?

ps.: this code act on this :

<div class="panel panel-default" id="results">
            <div class="panel-heading">
              <h1 class="panel-title">Panel title</h1>
              <div align="right"><button type="button" class="btn btn-lg btn-danger" onclick="close()">Fechar</button></div>
            </div>
            <div class="panel-body" id="content">
              Panel content
            </div>
          </div>
Kleber Mota
  • 8,521
  • 31
  • 94
  • 188
  • is there any error in the console – Arun P Johny Mar 06 '14 at 23:06
  • You can also verify that the selectors have matches ex: `$('#results').length` – sabof Mar 06 '14 at 23:10
  • And it does nothing? Neither hides the div nor empties the content? – Oscar Paz Mar 06 '14 at 23:10
  • I don't know how it works for you if you don't use jQuery, but the problem is probably that [`document` already has a property with name `close`](https://developer.mozilla.org/en-US/docs/Web/API/document.close), and that's the one which is being called in your inline event handler. If you rename the function (e.g. `xclose`) it works fine: http://jsbin.com/zunodako/1/edit?html,js,output. [One of the reasons not to use inline event handlers](http://stackoverflow.com/a/21975639/218196). – Felix Kling Mar 06 '14 at 23:11
  • ok, I rename the function and now it's working. Happens that i really don't had tried the vesion without jquery with the name close(). Sorry about that, but at least this experience serves to me and others (who find this question) pay more atention about this in the future. thanks for the help, anyway. – Kleber Mota Mar 06 '14 at 23:54
  • Added a proper answer. – Felix Kling Mar 07 '14 at 00:12

1 Answers1

1

The reason why your code doesn't work is because the inline event handler doesn't actually call the close function you defined.

Inside the event handler close refers to document.close, because the properties of the document is put in the scope of the event handler. And you can easily test this by use

onclick="console.log(close === document.close);"

as the handler. This is one of the reason why not to use inline event handlers.

Solutions:


Since you also admitted that you didn't run the non-jQuery version, it really has nothing to with using jQuery or not.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143