Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery reset append()

How can I reset after append() or before()? Please help. Many thanks in advance.

if((#tag_id).text() == 'something'){
   $(#tag_id).before("x");
}
// I want to reset the #tag_id to empty
$(#tag_id).val(''); // this doesn't work
like image 686
DGT Avatar asked Apr 09 '26 06:04

DGT


1 Answers

Your code example uses .before(). In this case, it would be better if .before() was given an element around x instead of just the text.

Then you could do this:

    // Wrap the "x" that you're inserting with <span> tags
$('#tag_id').before('<span>x</span>');

    // Use .prev() to reference the new <span> and remove it
$('#tag_id').prev().remove('span');​​​​​​​​​

Remember that using .before() does not place anything inside the #tag_id, but instead places it before the #tag_id.

If you meant for the content to go inside #tag_id but at the beginning, you would use .prepend().


If your code is using .append(), one option would be to keep a reference to the element you're appending, and use that to remove it later.

var $appendElem = $('<div>some appended element</div>');

$appendElem.appendTo('#tag_id');

Then you can remove it via the same reference.

$appendElem.remove();
// or
$appendElem.detach();
like image 131
user113716 Avatar answered Apr 11 '26 20:04

user113716