0

Here is the code to fetch a bunch of < a > tags which contains ID's and those ID's are use to fetch more data from the same file.. So to make this short as possible
index.html

var strURL ="co.php";
        var req = getXMLHTTP();
        if (req) {
            req.onreadystatechange = function() {
                if (req.status == 200) {
                    document.getElementById('content').innerHTML=req.responseText;
                }
            };
            req.open("GET", strURL, true);
            req.send(null);
        }

with that code I get my external file which contains something like this.
co.php

<div>
<a href="#" class="l" id="c1">Link 1</a>
<a href="#" class="l" id="c2">Link 2</a>
<a href="#" class="l" id="c3">Link 3</a>
<a href="#" class="l" id="c4">Link 4</a>
</div>
<script>
    $(".l").click(function () {
        alert($(this).attr("id"));
    });
</script>

and thats all that is for the co.php file... I can see the links but the script just doesn't work... and I wonder why... now that script is prefer to be in the index.html and not in the co.php file... but the result is the same.. if the script is in index.html it doesn't work, so how can I make it work? ... like I said I prefer that

<script>
    $(".l").click(function () {
        alert($(this).attr("id"));
    });
</script>

be in the index.html and make it work with the file that is from pull with ajax...

user2864740
  • 60,010
  • 15
  • 145
  • 220
Tanker
  • 1,178
  • 3
  • 16
  • 49

1 Answers1

2

Use event delegation:

$("#content").on('click','.l',function () {
    alert($(this).attr("id"));
});
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
  • holly mother of delegations! it worked!! thank you... the alert part is just to see if it works... I actually only need the ID number which is easy to get.. `var theid = $(this).attr('id').match(/\d+/)` and with that I get 1,2,3,4... so that I get more information from the same file co.php, which is working like a WebService without being one... Thank you... – Tanker Aug 14 '14 at 07:02