views:

374

answers:

2

Hi, I am stuck with this can anyone help me...

here is the html

<tr class="appendvalue">
  <td colspan="2">
     <asp:DropDownList ID="ddlSource" runat="server"></asp:DropDownList>
     <asp:TextBox ID="txtSourceValue" runat="server" CssClass="hide" />
     <a href="#" class="t12">add static value</a>
  </td>

here is the jquery

$(document).ready(function() {
        $('.appendvalue > td > a').live("click", function(e) {
            e.preventDefault();
            $(this).prev().prev().toggle();
            $(this).prev().toggle();
            $(this).toggle(function() { $(this).text("select from dropdown"); }, function() { $(this).text("add static value"); });
        });
    });

after the first click it only toggles' the anchor text not toggling dropdown and textbox..

+1  A: 

Try this instead:

$(document).ready(function() {
    $('.appendvalue > td > a').live("click", function(e) {
        e.preventDefault();
        var $this = $(this);
        $this.prevAll().toggle();
        $this.toggle(function() { $this.text("select from dropdown"); }, function() { $this.text("add static value"); });
    });
});
Doug Neiner
If you are going to use $(this) more then once cache it. var $this = $(this). Then use $this the rest of the places, means your not recreating jQuery objects.
PetersenDidIt
@petersendidit It was late. I claim immunity. :) Good catch, don't know why I didn't see that when I coded it up. Its been fixed. Thanks mate!
Doug Neiner
A: 

.toggle() is really a convenience method for click events, so using .toggle() within the click event handler of the same element is going to be problematic. Instead...

$(document).ready(function() {
    $('.appendvalue > td > a').live("click", function(e) {
        e.preventDefault();
        $(this).prevAll().toggle();
        if ($(this).parent().find("input").is(":hidden")) {
            $(this).text("add static value");
        } else {
            $(this).text("select from dropdown");
        }
    });
});
ctsears