/    Sign up×
Community /Pin to ProfileBookmark

change html of div

I am trying to change the content of divs, but it seems like after i click the first time, it won’t let me change anything else after that. so i will click a link, it will go bold, but then when i try to click another, nothing happens. here is my code:

[CODE]$(‘#allNav’).click(function() {
$(“#navPane”).html(“<li><div id=’allNav’><strong><a href=’#’>All</a></strong></div></li><li><div id=’photoNav’><a href=’#’>Photos</a></div></li><li><div id=’docsNav’><a href=’#’>Documents</a></div></li></ul> “);
});
$(‘#photoNav’).click(function() {
$(“#allTitle”).html(“Photos”);
$(“#navPane”).html(“<li><div id=’allNav’><a href=’#’>All</a></div></li><li><div id=’photoNav’><strong><a href=’#’>Photos</a></strong></div></li><li><div id=’docsNav’><a href=’#’>Documents</a></div></li></ul> “);
});

$(‘#docsNav’).click(function() {
$(“#allTitle”).html(“Documents”);
$(“#navPane”).html(“<li><div id=’allNav’><a href=’#’>All</a></div></li><li><div id=’photoNav’><a href=’#’>Photos</a></div></li><li><div id=’docsNav’><strong><a href=’#’>Documents</a></strong></div></li></ul> “);
});[/CODE]

[CODE]<div id=”navPane”>
<li><div id=”allNav”><strong><a href=”#”>~All</a></strong></div></li>
<li><div id=”photoNav”><a href=”#”>Photos</a></div></li>
<li><div id=”docsNav”><a href=”#”>Documents</a></div></li>
</div>[/CODE]

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@Nemesis02Jun 16.2012 — The reason that's happening is because you're removing the link which you had the event attached to. when you remove that anchor link, you remove the event as well and you have to reattach it.
Copy linkTweet thisAlerts:
@droidusauthorJun 16.2012 — I am not sure what you mean. I am replacing the text inside the div with exactly was in there before.
Copy linkTweet thisAlerts:
@Nemesis02Jun 16.2012 — That doesn't really matter if it's the same or not, think of it this way, easy tag is unique and when you attach an event, you attach it to the tag, you remove that tag you remove everything associated with, including events whether you replaced it with the same text or not. What you'll have to do is store that function to a variable, attach the function by variable name, that at the end of that function, run that same command to attach the event again.
Copy linkTweet thisAlerts:
@droidusauthorJun 16.2012 — I am still not sure what you mean. i have the tag, <div id="navPane">. this holds the event. that should not change. only what inside should. would you possibly be able to show me with my code, or a short example?
Copy linkTweet thisAlerts:
@Nemesis02Jun 16.2012 — You should look carefully at your code, you attached your event to allnav which is inside navpane, when you do $("#navpane").html you basically emptied out the contents of the tag, events included and replaced it with what you passed to the html function.
Copy linkTweet thisAlerts:
@droidusauthorJun 16.2012 — so I am basically loosing the attached events with <div id='allNav'>? i guess I am confused, because I don't see the event that is physically attached. i understand that the event is when you click on it, but it should not get overridden, since each link is defined separately. and those links exist in each of the new htmls that are produced.
Copy linkTweet thisAlerts:
@Nemesis02Jun 16.2012 — Yes, you lost the event cause the original element it was attached to was replaced when you did the html call so you'll have to re-register the event with that element. There is no to physically see that you have an event attached to an element, it's more or less registered with the browser itself, is up to you to know that an event should be attached to it since you coded it. If an event is not triggering like it should, it's an indication that the event somehow was unregistered, and you happen to do that when you removed allnav and added it again.
Copy linkTweet thisAlerts:
@xelawhoJun 17.2012 — ... but if you just want to turn a link bold when you click it, aren't you better off doing that with css?
Copy linkTweet thisAlerts:
@Nemesis02Jun 17.2012 — ... but if you just want to turn a link bold when you click it, aren't you better off doing that with css?[/QUOTE]

Yes. Make make a class called bolded with the style font-weight: bold; and then use addClass removeClass to add the bolded class to the text.
Copy linkTweet thisAlerts:
@xelawhoJun 17.2012 — or just make a visited pseudo class for the relevant links - no js/jQ required...
Copy linkTweet thisAlerts:
@droidusauthorJun 17.2012 — it's not only that that i'm changing, but it's also some more content of the page.
×

Success!

Help @droidus spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...