0

I have replaced chosen replaced and inserted dom elements this way:

$(".editLink").click(function () {
    var $temp = $(".editLink").parent().prev().find("label").html();

    var $tempBox = $(".editLink").parent().prev().find("label").replaceWith("<input type='text' name='dataUpdate'/>");

    $tempBox.insertAfter("<input type='submit' value='Update'/>");
});

However, changes flicker,..and dont persist. They only persist for one second..(I guess only for the time of the event. How can I make them persist forever ?

UPDATE:

<table width="400px">
<form action="/Admin/Update?Length=4" method="post">
<tr>
    <td>
        <label for="fadsf">
            fadsf</label>
    </td>
    <td>
        <a class="editLink" href="/Admin/MenuManagement?Length=5">Edit</a>
    </td>
    <td>
        <a class="deleteLink" data-ajax="true" data-ajax-mode="replace" data-ajax-update="#1"
            href="/Admin/Delete?deleteID=1&amp;unDelete=1" id="1" title="0">Delete</a>
    </td>
</tr>
</form>

3
  • They persist forever by default. If you get different behavior then something else in your page is also modifying the DOM. Commented Mar 5, 2012 at 13:43
  • Could you please provide us you HTML code? It seems like another event callback is reverting changes back. Commented Mar 5, 2012 at 13:44
  • make a demo that replicates issue...provided info is not enough to troublleshoot Commented Mar 5, 2012 at 13:50

2 Answers 2

1

I'll guess you suggest they only persist for the time of the event because you are binding to an <a> tag which causes the page to reload or navigate somewhere else. You need to prevent that default behavior like so:

$(".editLink").click(function (e) {
   ... other code

   $(e).preventDefault();

   ... other code
});

And the documentation - https://developer.mozilla.org/en/DOM/event.preventDefault

UPDATE - Based on the HTML you have now provided

Your HTML does not look valid. You cannot nest a form tag in between a table tag and a tr tag and expect it to work. First off, move the form tag outside of the table. Secondly, close the table with a matching </table> tag. Maybe that will help.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

will throw error in jQuery....incorrect implementaion api.jquery.com/event.preventDefault
@charlietfl updated implementation, thanks for catching that.
I dont think it matters. I create many forms with each row..I must use it that way
1

returning false from the click handler will prevent the browser following the url in the href

$(".editLink").click(function () {
    var $temp = $(".editLink").parent().prev().find("label").html();

    var $tempBox = $(".editLink").parent().prev().find("label").replaceWith("<input type='text' name='dataUpdate'/>");

    $tempBox.insertAfter("<input type='submit' value='Update'/>");

    return false;
});

You can also use event.preventdefault() http://api.jquery.com/event.preventDefault/

  $(".editLink").click(function (event) {
         event.preventDefault();
         /* your code*/

  })

7 Comments

I see..thats why i was wrong.. I also use insertAfter..but no button appears ..why is that?
insertAfter( jQuerySelector) ....you have a string...not sure what you are trying to do api.jquery.com/insertAfter
I am trying to get the element that I replaced, and insert after it another element..A submit button
I think this is what you want: $("<input type='submit' value='Update'/>").insertAfter($tempBox);
I am trying to insert this: $("<input type='submit' value='Update' />").insertAfter($tempBox); ... submit button after the textbox
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.