0

Hi all I have a problem with anchors and dynamic content loaded thought $.ajax().

Let suppose this is my jQuery code:

$("a.false").live("click", function() {
    var data = $(this).attr("href");
    $("div#text").hide("fast");
    $("div#loader").show("fast");
    $.ajax({
        type: "GET",
        url: "callback.php",
        data: data,
        dataType: "html",
        cache: false,
        success: function(result){
            $("div#loader").hide("fast");
            $("input#data").val(data);
            $("div#text").html(result);
            $("div#text").show("fast");
        }
    });
});

I think everybody knows what means this code and I'll no explain it.

callback.php answer is:

<div class="name">
    <div class="replic_id">
        <input type="hidden" id="replica_1" name="replica_1" value="1" />
        <a id="rp1">#1</a>
    </div>
    <div class="names">Name 1</div>
    <div class="replicas">Replica 1</div>
</div>
<div class="name">
    <div class="replic_id">
        <input type="hidden" id="replica_2" name="replica_2" value="2" />
        <a id="rp2">#2</a>
    </div>
    <div class="names">Name 2</div>
    <div class="replicas">Replica 2</div>
</div>

And now I javascript variable "data" value is = "movie=scarymovie#255" the page must start from anchor #255 but it starts from the begin. How I can fix it to start from anchor 255, no from begin?

with regards ;]

Fixed: Correct a IDs

1 Answers1

1

It is easier to send back the anchor as its own value and just use:

window.location.hash = '#255';

It is worth noting that even though it may work, 255 is not a valid value for an ID attribute in HTML: What are valid values for the id attribute in HTML?

Edit: fixed a typo in the variable name

Community
  • 1
  • 1
Jeremy Battle
  • 1,638
  • 13
  • 18
  • I fix the ids but I have a problems with this js code. Error is when the script excute... screen -> http://bit.ly/guqS0S –  Mar 07 '11 at 04:11
  • @T0m3kk editted the hash value, it needs to have a hash mark in front of it like '#anchorName' let me know if that works for you. – Jeremy Battle Mar 07 '11 at 04:16
  • but how I can set in on loading of page after `$("div#text").html(result);` and to set the start anchor #rp255 ? –  Mar 07 '11 at 04:24
  • If the data value is "movie=scarymovie#rp255" like you mention above you can get rp255 by doing something like: var newLocation = data.split('#')[1]; and then doing window.location.hash = '#' + newLocation; – Jeremy Battle Mar 07 '11 at 04:45