This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to swap out div tags programmatically with JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to swap out div tags programmatically with JavaScript" Watch "How to swap out div tags programmatically with JavaScript" New topic
Author

How to swap out div tags programmatically with JavaScript

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Is there a straight-forward way to, say, when the user clicks a nav button, to swap out the text within a div tag?

What I'm trying to do is to update the page the user visits, as he clicks various nav buttons, by swapping out just the part(s) of the page that change (the main content div, in this case) without having to create a separate page with the nav bar and all the other parts for each page.

So, for an example, say you have a "home page" and the user clicks the "Testimonials" link. In my case, much of the "home page" would be the same for each page -- the nav bar, footer, etc.. So, all I'd need to do is to somehow "swap in" the Testimonials text for the main div on the "home page".

I looked at jQuery but couldn't figure it out with the books I looked at, so I'm back to JavaScript.

Look forward to any suggestions or code samples.

Thanks.

-- Mike
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You can have the code there and just change the visibility [display = "none|block"] or you can fetch the content from the server using Ajax.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

jQuery would make this almost trivial.

For example, to load a div from a URL:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Thanks Bear, but how would you do the same thing in JS? A major pain?

----------

Also, you're appending with your jQuery code, right? If that's the case, how would you "replace" the div's current contents?

Thanks.

- Mike
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60802
    
  65

Well, that is JavaScript. jQuery isn't a new language, just a JavaScript library.

And yes, doing Ajax "by hand" is many more lines of code. You can find examples all over the web.

I highly discourage doing Ajax without the benefit of a library. There are too many nuances and pitfalls, and life is just too short to screw with them.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1043
Thanks Bear.

I appreciate, as always, your reply.

Best,

-- Mike
 
 
subject: How to swap out div tags programmatically with JavaScript
 
Similar Threads
how to know which servlet/jsp calling a particular servlet
Checking for nulls in JSP if user bypasses struts
how is html evaluated, ie attributes..
Link question
Div tag - changing main content page