0

File 1.php load an external php file (2.php) inside a div called

<div id="myoutput"></div>

I load 2.php (template) using load().

this template contain a line like

<a href="#" id="showall">all</a>

but when i click "all" the script doesn't work.

if i put

<a href="#" id="showall">all</a>

inside 1.php all work fine. inside 2.php not.

this is the click function

$('#showall').click(function() {
            $( "#myoutput" ).hide();
            $('.rowresult').show();
        });

How to access to the DOM on an external file?

flexicon
  • 102
  • 1
  • 11
  • Use [Event delegation](http://learn.jquery.com/events/event-delegation/) – charlietfl Feb 12 '17 at 13:57
  • my question is different. I don't want to use a different method to click a link. Script work fine if i put the html inside 1.php. if i try to select an html id="" of an external file, loaded with load(), the script doesn't work. i want to read and manipulate external DOM, loaded in an external html file, where the function that call is inside 1.php and not in 2.php – flexicon Feb 12 '17 at 14:15
  • There is nothing different about that and event delegation will take care of it for you...that is what it is used for. The alternative is to put your code inside the load() completion callback so it runs when that element actually exists – charlietfl Feb 12 '17 at 14:34
  • Just change to `$(document).on('click','#showall',function() { $( "#myoutput" ).hide(); $('.rowresult').show(); });` – charlietfl Feb 12 '17 at 14:38
  • RIGHT!!! GREAT !!!! work fine!! Tell me please... why $( "#showall" ).on doesn't work and document yes? #showall is not on the DOM? – flexicon Feb 12 '17 at 14:42
  • Read the link(s) I provided above. It doesn't work because that element doesn't exist at the time you run the code. You can't add a listener to something that doesn't exist – charlietfl Feb 12 '17 at 14:43

0 Answers0