0

Hey guys, quick question. I have a div that on click will execute a jquery function onclick, and then replace the div with its opposite div which onclick will replace back to the first div just like the favorite function on this site. I am having a simple problem I hope someone can spot or explain to me it is not possible. The function works with the initial two divs that are served directly, but when I click on either of the original two divs, and the function executes and replaces the div with the corresponding div the replacement div does not execute the function. I have to refresh the page to get the original div, which is identical to execute the function. Is the div actually replaced on the page with append, or does it just visually show it? Any advice appreciated.

original div

<div class="unfavorite"><img id="unfavorite_img" src="images/favorite2.png" /></div>

javascript div replacement

 $(".favorite").remove();
 $(".favoriteholder").append('<div class="unfavorite"><img id="unfavorite_img" src="images/favorite2.png" /></div>');

1 Answer 1

2

Instead of removing, hide them. Because, once you remove the DOM element, until the page is refreshed that DOM element is gone!, so you can't actually access it.

Other alternative is, have a temp div that holds this content (content for both divs) and get the content from the div to append to the present div or to create the removed div

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

3 Comments

that is what I thought, but then how can I get the appended div to work is that possible? I don't care about the removed one, I just want the one that is appended to trigger the function. Do I have to hide it then fade it in?
you have to care about the removed one because you need it when you click on the present div, isn't that right?
o so I need both? What I was doing was removing one and appending the other then trying to click on the appended one, but I guess your saying that I need to have the appended one preloaded and fade it in then fade the other out? I wanted the same effect you have on this site when favoriting a topic.

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.