tags:

views:

27

answers:

2

Using JQuery, and still a little new to it, trying to change an Link into a Drop Down of options, whi8ch the user can select from, and upon selecting a choice, it returns to a hyperlink with the text of the selection.

So far, I have:

$("a.timeChange").click(function(e){
    e.preventDefault();
    $stamp = $(this).text();
    $.get("tstampoffsetter.php", { t: $stamp, a: 1 }, function(data){
        alert(data);
        $(this).replaceWith(data);
    });
});
$("#selectTimeStamp").change(function(){
    var i = $("#selectTimeStamp :selected").text();
    $(this).replaceWith("<a href='#'>" + i + "</a>");
});

The HTML is as follows:

<a href='#' class='timeChange'>Mon, 13 Sep 2010 04:20:45 -0700</a>

The data that is returned from the AJAX call is:

<select id="selectTimeStamp">
   <option value='1284445245'>Mon, 13 Sep 2010 23:20:45 +1200</option>
   <option value='1284441645'>Mon, 13 Sep 2010 22:20:45 +1100</option>
   <option value='1284438045'>Mon, 13 Sep 2010 21:20:45 +1000</option>
   <option value='1284434445'>Mon, 13 Sep 2010 20:20:45 +0900</option>
   <option value='1284430845'>Mon, 13 Sep 2010 19:20:45 +0800</option>
   <option value='1284427245'>Mon, 13 Sep 2010 18:20:45 +0700</option>
   <option value='1284423645'>Mon, 13 Sep 2010 17:20:45 +0600</option>
   <option value='1284420045'>Mon, 13 Sep 2010 16:20:45 +0500</option>
   <option value='1284416445'>Mon, 13 Sep 2010 15:20:45 +0400</option>
   <option value='1284412845'>Mon, 13 Sep 2010 14:20:45 +0300</option>
   <option value='1284409245'>Mon, 13 Sep 2010 13:20:45 +0200</option>
   <option value='1284405645'>Mon, 13 Sep 2010 12:20:45 +0100</option>
   <option value='1284402045'>Mon, 13 Sep 2010 11:20:45 +0000</option>
   <option value='1284398445'>Mon, 13 Sep 2010 10:20:45 -0100</option>
   <option value='1284394845'>Mon, 13 Sep 2010 09:20:45 -0200</option>
   <option value='1284391245'>Mon, 13 Sep 2010 08:20:45 -0300</option>
   <option value='1284387645'>Mon, 13 Sep 2010 07:20:45 -0400</option>
   <option value='1284384045'>Mon, 13 Sep 2010 06:20:45 -0500</option>
   <option value='1284380445'>Mon, 13 Sep 2010 05:20:45 -0600</option>
   <option SELECTED value='1284376845'>Mon, 13 Sep 2010 04:20:45 -0700</option>
   <option value='1284373245'>Mon, 13 Sep 2010 03:20:45 -0800</option>
   <option value='1284369645'>Mon, 13 Sep 2010 02:20:45 -0900</option>
   <option value='1284366045'>Mon, 13 Sep 2010 01:20:45 -1000</option>
   <option value='1284362445'>Mon, 13 Sep 2010 00:20:45 -1100</option>
</select>

When the link is clicked, I get no error, yet the link does not become a drop down. I see the GET request via FireBug and it's returning the data, Status 200 OK 44ms. After I OK the Alert window, I see no change and no errors. Thanks for any help or advice you might be able to provide.

A: 

It's because this doesn't refer to the link in the $.get() success callback (it refers to the jQuery ajax object), you need to keep a reference to the anchor you clicked, like this:

$("a.timeChange").live("click", function(e){
    e.preventDefault();
    var link = $(this), $stamp = link.text();
    $.get("tstampoffsetter.php", { t: $stamp, a: 1 }, function(data){
       link.replaceWith(data);
    });
});
$("#selectTimeStamp").live("change", function(){
    var i = $("#selectTimeStamp :selected").text();
    $(this).replaceWith("<a href='#' class='timeChange'>" + i + "</a>");
});

The this change fixes the first problem, as per your comment the second is binding to newly created elements you'll need to use .live() and when re-creating the link be sure it has the timeChange class (so the .live() selector matches).

Nick Craver
Thanks Nick! This fixed that first function. My second function is not applying to the new SELECT object. Do I need to attach that after I replace?
Twisty
@Twisty - Oh, use `.live()` for that, e.g. `$("#selectTimeStamp").live("change", function(){` for the select, `$("a.timeChange").live("click", function(e){` for the anchor, I'll update in a moment.
Nick Craver
Thanks! Perfect fix for that. Guffa beat you to it. All working just as I wanted.
Twisty
@Twisty - Be careful, his solution and your original code doesn't add the `timeChange` class back, clicking the link again (created by the select going away) won't work :) Make sure to add the class back!
Nick Craver
You're right, I missed that. Was scratching my head at why it was not working a second time. New code:
Twisty
$("a.timeChange").live('click', function(e){ e.preventDefault(); var link = $(this), $stamp = $(this).text(); $.get("tstampoffsetter.php", { t: $stamp, a: 1 }, function(data){ link.replaceWith(data); }); }); $("#selectTimeStamp").live('change', function(){ //var i = $("#selectTimeStamp :selected").text(); var selObj = $(this); selObj.replaceWith("<a href='#'>" + $("#selectTimeStamp :selected").text() + "</a>"); });
Twisty
Copied the wrong version of the code: $("#selectTimeStamp").live('change', function(){ var i = $("#selectTimeStamp :selected").text(); var selObj = $(this); selObj.replaceWith("<a href='#' class='timeChange'>" + $("#selectTimeStamp :selected").text() + "</a>"); });
Twisty
Any thoughts on reverting the event if the user clicks off the Select object? E.G User clicks on link, and then clicks on another section of the page.
Twisty
@Twisty - Change `.live("change"` to `.live("change blur"` so it fires on both events.
Nick Craver
No go with the following code: $("#selectTimeStamp").live('change blur', function(event){ if(event.type == 'blur'){ return false; } var i = $("#selectTimeStamp :selected").text(); var selObj = $(this); selObj.replaceWith("<a href='#' class='timeChange'>" + $("#selectTimeStamp :selected").text() + "</a>"); });--- Will keep poking at it, I think you gave me the right direction.
Twisty
@Twisty - If you want to put the link back whenever clicking of the `<select>` I'd just remove that if check and `return false` portion.
Nick Craver
@Nick - Tried it a few ways, just doesn't seem to catch the blur event: $("#selectTimeStamp").live('change blur', function(){ var selObj = $(this); selObj.replaceWith("<a href='#' class='timeChange'>" + $("#selectTimeStamp :selected").text() + "</a>"); return false; });
Twisty
A: 

You can use the live method, so that the events will also be attached to elements that doesn't exist yet:

$("a.timeChange").live('click', function(e){
    e.preventDefault();
    var $link = $(this)
    $.get("tstampoffsetter.php", { t: $link.text(), a: 1 }, function(data){
        alert(data);
        $link.replaceWith(data);
    });
});
$("#selectTimeStamp").live('change', function(){
    var text = $("#selectTimeStamp :selected").text();
    $(this).replaceWith('<a href="#">' + text + '</a>');
});
Guffa
Guffa! You and Nick rocked it! Thanks, using .live() worked perfectly for the functions that would attach to the new Select Object.
Twisty