tags:

views:

66

answers:

4

i have page in php and all over page i am using < a tag to link

for example

 <?php for($i=1;$i<=5;$++) {?>
 <a href="abc.php?ref=<?php echo $i ?>"CLick me No. <?php echo $i ?> </a>
 <?php } ?>

what i want to do is

once we click on link jquery load function should called, like

 $('#div1').load('abc.php?ref=1'null, function() {
  });

but i can't change PHP loop and < a tag ...

Thanks

A: 

You mean something like this:

<a href="abc.php?ref=<?php echo $i ?>" id="yourLink">"CLick me No. <?php echo $i ?> </a>


$('#yourLink').click(function() {
    $('#div1').load('abc.php?ref=1'null, function() {
    });
});

Bobby

Bobby
Same here, he can't add that ID to the <a> tag
MaxiWheat
yes but as you see in my code there is different links and each link is passing different query string, how can i manage it so that it get original link with query string from <a tag and load it....
air
@MaxiWheat, you're right, I missed that one, sorry.@air, you could add the handler to _every_ link in the page, and check the target of the link.
Bobby
A: 

Give each anchor a common class and an attribute that identifies the value of $i:

<a href="..." anchor-id="<?= $i ?>" class="numbered-anchor">...</a>

Then attach an on-click function to them:

$('a.numbered-anchors').click(function(e) {
  var i = $(this).attr('anchor-id');
  $('#div' + i).load(...);
});
Jeff Ober
He said he cant't change the php or <a> tag, so he can't add a class
MaxiWheat
Can he provide the anchor tags' parent element? If they are wrapped in a div that can be identified by a CSS selector, he can get at them.
Jeff Ober
A: 

Something like this maybe :

$('a[href^="abc.php"]').click(function(event){
  //do whatever you want
   $('#div1').load(event.target.href, null, function() {});

});
MaxiWheat
Now should load the target of each <a> tag
MaxiWheat
A: 

PHP:

<?php for($i=1;$i<=5;$++) {?>
  <a class="anchor-click" href="#" id="<?php echo $i ?>">CLick me No. <?php echo $i ?></a><br />
<?php } ?>

JS

$(function(){
   $(".anchor-click").bind("click", function(event){
      event.stopPropagation();
      $('#div1').load("abc.php?ref="+$(this).attr("id"), null, function() {

      });
   });
});
andres descalzo
I don't know JQuery, but your fix is not very efficient and you recommend using numbers as ids... You should change it like this: put the <?php .. ?> inside a <div id="x"> and then loop through $('#x a') and set the event. You should also define the event as a function variable and use the variable when attaching the event to avoid loading the JS space. That would get you my upvote ;)
Tom
Instead of <div id="x"> use <div id="div1"> and $('#div1'), as the asker wanted.
Tom
you can take the example as an approximation to the solution.if you put in your strictest, you should also mention that the example of "@air" is not closed the anchor tag.
andres descalzo