toggle layers and background-image of clicked tab

C

Chris

I am trying to create a three tab menu where each tab toggles one of
three layers. The first span tab shows layer 1, hides layer 2/3, and
so on.

I am using javascript created by dreamweaver which seems over the top
and doesn't achieve what I want to anyway. I can toggle the layers but
I also want to toggle the background image of the selected tab.

If someone had an example of how to toggle through the layers and
change the background image of the corresponding tab I would be very
grateful.

Many thanks,

Chris
 
T

Thomas Allen

I am trying to create a three tab menu where each tab toggles one of
three layers. The first span tab shows layer 1, hides layer 2/3, and
so on.

I am using javascript created by dreamweaver which seems over the top
and doesn't achieve what I want to anyway. I can toggle the layers but
I also want to toggle the background image of the selected tab.

If someone had an example of how to toggle through the layers and
change the background image of the corresponding tab I would be very
grateful.

Many thanks,

Chris

You may want to give this a try...
http://jqueryui.com/demos/tabs/

Thomas
 
R

RobG

I am trying to create a three tab menu where each tab toggles one of
three layers. The first span tab shows layer 1, hides layer 2/3, and
so on.

I am using javascript created by dreamweaver which seems over the top
and doesn't achieve what I want to anyway. I can toggle the layers but
I also want to toggle the background image of the selected tab.

If someone had an example of how to toggle through the layers and
change the background image of the corresponding tab I would be very
grateful.

Presumably your tabs are div elements. You can put an onclick
listener on each div (or on a div that encloses the tab divs) and sets
the background image of the clicked tab to the "selected" background
and the previously selected tab to the default (not selected)
background.

That is most easily achieved by adding and removing a class value,
then your page designers can play with the class rules to change the
look of the tabs and you don't have to change your script.

Here's a short example:

<head>
<title>Tabe example</title>
<style type="text/css">
#tabWrapper {
border: 1px solid red;
height: 52px;
}
.tab {
float: left;
border: 1px solid blue;
width: 100px;
height: 50px;
}
.selected {
background-color: #CCCC33;
}
</style>
<script type="text/javascript">

// Add a className
function addClass(el, className){
var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
if (!re.test(el.className)) {
el.className += (' ' + className);
}
}

// Remove a className
function removeClass(el, className){
if (hasClassname(el, className)) {
var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
el.className = (el.className.replace(re, ' '));
}
}

// See if el has className
function hasClassname(el, className){
var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
return (re.test(el.className));
}


function setTab(e, wrapper) {
var el = e.target || e.srcElement;
var tabs = wrapper.childNodes;
var re = /^tab\d+/;
for (var i=0, iLen=tabs.length; i<iLen; i++) {
if (re.test(tabs.id)) {
removeClass(tabs, 'selected');
}
}
addClass(el, ' selected');
}

</script>

</head>
<body>

<div id="tabWrapper" onclick="setTab(event, this);">
<div id="tab0" class="tab selected">Tab 0</div>
<div id="tab1" class="tab">Tab 1</div>
<div id="tab2" class="tab">Tab 2</div>
</div>

</body>
 
T

Thomas 'PointedEars' Lahn

Thomas said:
I am trying to create a three tab menu where each tab toggles one of
three layers. The first span tab shows layer 1, hides layer 2/3, and
so on.

I am using javascript created by dreamweaver which seems over the top
and doesn't achieve what I want to anyway. [...]

You may want to give this a try...
http://jqueryui.com/demos/tabs/

OP: Ignore this fellow wannabe.

<http://groups.google.com/groups?as_q=jQuery&as_ugroup=comp.lang.javascript&scoring=d&filter=0>
<http://jibbering.com/faq/#posting>


PointedEars
 
R

RobG

RobG ha scritto: [...]
Here's a short example:
<head>
  <title>Tabe example</title>
  <style type="text/css">
  #tabWrapper {
    border: 1px solid red;
    height: 52px;
  }
  .tab {
    float: left;
    border: 1px solid blue;
    width: 100px;
    height: 50px;
  }
  .selected {
    background-color: #CCCC33;
  }
  </style>
  <script type="text/javascript">
// Add a className
function addClass(el, className){
  var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
  if (!re.test(el.className)) {
    el.className += (' ' + className);
  }
}
// Remove a className
function removeClass(el, className){
  if (hasClassname(el, className)) {
    var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
    el.className = (el.className.replace(re, ' '));
  }
}
// See if el has className
function hasClassname(el, className){
  var re = new RegExp('(^|\\s)'+className+'(\\s|$)');
  return (re.test(el.className));
}
function setTab(e, wrapper) {
  var el = e.target || e.srcElement;
  var tabs = wrapper.childNodes;
  var re = /^tab\d+/;
  for (var i=0, iLen=tabs.length; i<iLen; i++) {
    if (re.test(tabs.id)) {
      removeClass(tabs, 'selected');
    }
  }
  addClass(el, ' selected');


If you click outside the three tabs but inside the "container", it
changes style and the three divs are not clickable anymore.
so I replaced with:
if (el.id!="tabWrapper")  addClass(el, ' selected');
Correct?> }


Yes, but there are a few other gotchas - if you put other elements
inside the tabs, you'll need to do something like go up to the first
div element and use that (so el is the parent div of event.target).

Also it would be better to only respond if the click comes from a tab,
so:

if ( re.test(el.id) ) {
// do the rest
}

If you keep the markup simple, the function can stay simple. Adding
complexity will add complexity to either the function or how it is
attached. There are many strategies, pick the one that works best for
you. :)
 
C

Chris

Thomas said:
I am trying to create a three tab menu where each tab toggles one of
three layers. The first span tab shows layer 1, hides layer 2/3, and
so on.
I am using javascript created by dreamweaver which seems over the top
and doesn't achieve what I want to anyway. [...]
You may want to give this a try...
http://jqueryui.com/demos/tabs/

OP: Ignore this fellow wannabe.

<http://groups.google.com/groups?as_q=jQuery&as_ugroup=comp.lang.javas...>
<http://jibbering.com/faq/#posting>

PointedEars

Thanks everybody for all your help and taking the time! The new
feature will be in place in our redesign of lagancollege.com in the
next two weeks.

Regards,

Chris
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Good point. Those tabs clearly don't work.

They do work, in certain environments, but given the quality of the source
code (or rather the lack thereof), they work there by coincidence. The next
browser version, or the next yet unknown, but standards-compliant, user
agent can break the thing.

As someone new to this newsgroup, you would be well-advised to read previous
discussions on the subject before getting your mouth this full:

<http://jibbering.com/faq/#posting>


PointedEars
 
T

Thomas Allen

They do work, in certain environments, but given the quality of the source
code (or rather the lack thereof), they work there by coincidence.  Thenext
browser version, or the next yet unknown, but standards-compliant, user
agent can break the thing.

As someone new to this newsgroup, you would be well-advised to read previous
discussions on the subject before getting your mouth this full:

<http://jibbering.com/faq/#posting>

PointedEars

Call me optimistic, but I expect the sizable jQuery UI project to do a
good job of keeping up with new browsers as they emerge. Either way,
could you please point me to the relevant part of the linked FAQ? As
far as I can tell, my advice does not fall under either of the "What
Not to Post" section:

* Do not post job postings. Job postings should go to an appropriate
regional jobs group.
* Do not post copyright material without permission from the copyright
holder.

We may disagree in our approach to getting things done, but that's not
to say that suggesting a functional library is without merit.

Thomas
 
T

Thomas 'PointedEars' Lahn

Thomas said:
Thomas said:
Thomas said:
Thomas 'PointedEars' Lahn wrote:
OP: Ignore this fellow wannabe.
Good point. Those tabs clearly don't work.
They do work, in certain environments, but given the quality of the
source code (or rather the lack thereof), they work there by
coincidence. The next browser version, or the next yet unknown, but
standards-compliant, user agent can break the thing.

As someone new to this newsgroup, you would be well-advised to read
previous discussions on the subject before getting your mouth this
full:

<http://jibbering.com/faq/#posting>
[...]

Please stop your mindless full-quoting and, in particular, your quoting
signatures when not relevant to your followup.
Call me optimistic, but I expect the sizable jQuery UI project to do a
good job of keeping up with new browsers as they emerge.

The flaw in that logic is that you don't really know when previously unknown
UAs have emerged until it is too late (i.e. not-too-disappointed visitors
are reporting bugs to the site owner, and not-too-disappointed jQuery users
identify them as jQuery-related and report them to John Resig and his
followers). The probability of that chain of events happening, let alone
its outcome having effect eventually, is so marginally small that it can be
ruled out right there.

Therefore, responsible script code is written so that it does not break when
exposed to an unknown but not-too-unpredictable environment in the first
place, which is entirely possible (as many people, including me, have
already demonstrated). The jQuery way, on the other hand, is to update the
source code every time Resig or maybe one of his (apparently mostly
incompetent) followers learn about a new browser or a new browser version;
that way lies madness. We can observe the madness right now with Resig
rushing a new jQuery release before IE 8 gets too widely distributed and the
conceptual flaws in jQuery would have become apparent (or so they must hope.)
Either way, could you please point me to the relevant part of the linked
FAQ?

It's the part that says, in a nutshell, that you should read before you post.


PointedEars
 
T

Thomas Allen

It's the part that says, in a nutshell, that you should read before you post.

But you can't expect omniscience out of everybody. We are here to help
and to teach each other.

Thomas
 
D

David Mark

But you can't expect omniscience out of everybody. We are here to help
and to teach each other.

Your lesson for the day is that the "sizable jQuery UI project" is a
complete waste of time. It's built on top of jQuery, which is a
wobbly moving target (see other recent threads about jQuery.) It is
hardly a foundation for cross-browser widgets (or cross-browser
anything.)

Told you not to build castles in a swamp.
 
T

Thomas Allen

Your lesson for the day is that the "sizable jQuery UI project" is a
complete waste of time.  It's built on top of jQuery, which is a
wobbly moving target (see other recent threads about jQuery.)  It is
hardly a foundation for cross-browser widgets (or cross-browser
anything.)

Told you not to build castles in a swamp.

I hear you railing against jQuery, and I wonder to myself...which, if
any, JavaScript librar(ies) do you prefer? If you had to pick one from
the field, which would it be? It seems to me that you believe the only
sensible choice to be custom code, which, while possibly the best
choice if you have enough time, certainly is not appropriate for every
project.

Thomas
 
T

The Natural Philosopher

Thomas said:
I hear you railing against jQuery, and I wonder to myself...which, if
any, JavaScript librar(ies) do you prefer? If you had to pick one from
the field, which would it be? It seems to me that you believe the only
sensible choice to be custom code, which, while possibly the best
choice if you have enough time, certainly is not appropriate for every
project.

Thomas
If all you want to do is a little background toggling, its more than
adequate..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top