Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need help with simple script

Posted on 2006-05-06
11
Medium Priority
?
454 Views
Last Modified: 2008-02-01
I need a very simple/bare bones javascript that will:

1) Do a getdocumentID of a list menu, looking for an <li> element with a class or id of "selected"
2) Upon locating that element, the script will change the classes of the previous and next sibling <li> elements of "selected"

That's it.
0
Comment
Question by:aznprncritic
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 17

Expert Comment

by:Cem Türk
ID: 16625478
can you post an example html?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 16625574
<html>
<head>
<script language='JavaScript' type='text/javascript'>
  function locate( id ) {
    var item = document.getElementById( id );
    for ( var i = 0; i < item.length; i++ ) {
      if ( item[ i ].selected ) {
        alert( 'Selected: ' + item[ i ].value );
      }
    }
  }
</script>
</head>
<body>
<select id='ListID' onchange='locate("ListID")'>
  <option>Please pick one</option>
  <option>one</option>
  <option>two</option>
  <option>three</option>
</select>
</body>
</html>
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 16625658
Sorry, didn't see the request to update item[ i - 1 ] & item[ i + 1]


<html>
<head>
<script language='JavaScript' type='text/javascript'>
  function locate( id ) {
    var item = document.getElementById( id );
    for ( var i = 0; i < item.length; i++ ) {
      if ( item[ i ].selected ) {
        alert( 'Selected: ' + item[ i ].value );
        if ( i > 0 ) {
          item[ i - 1 ].innerHTML = 'Before';
        }
        if ( i + 1 < item.length ) {
          item[ i + 1 ].innerHTML = 'After';
        }
      }
    }
  }
</script>
</head>
<body>
<select id='ListID' onchange='locate("ListID")'>
  <option>Please pick one</option>
  <option>one</option>
  <option>two</option>
  <option>three</option>
</select>
</body>
</html>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:aznprncritic
ID: 16625919
Oops, sorry, I thought I'd posted the HTML but i guess not:

<ul id="subNav">
    <li id="overview"><a href="index.html" class="selected" title="Overview">Overview</a></li>
    <li id="place"><a href="javascript:ajaxpage('aplace-content.html','content');" title="A
        Place">A Place</a></li>
    <li id="program"><a href="javascript:ajaxpage('aprogram-content.html','content');" title="A Program">A
        Program</a></li>
    <li id="team"><a href="javascript:ajaxpage('ateam-content.html','content');" title="A Team">A
        Team</a></li>
</ul>

Also, if either of the first or last menu items have anchors in the "selected" state, only their next or previous sibling, respectively, would need to be changed, since they appear at both ends of the menu.  

Last but not least, here's a url you can look at for a better idea.  Hover over the links to see the left and right border effects, and how, if the link is already in the selected state, the previous and next siblings utilize that border instead of creating a 2px border.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 16625982
The URL is missing...
0
 

Author Comment

by:aznprncritic
ID: 16626185
Hmmm.  Let's try that again:

http://cwi.yellowworld.org/subnavigation.html
0
 

Author Comment

by:aznprncritic
ID: 16639197
Anyone?
0
 
LVL 1

Expert Comment

by:Nightfalcon
ID: 16639562
Hi,

I changed the code a bit so that the class of the option will change.
The class of the unselected will change to 'notselected', the class of the selected to 'selected'.

Here you go:

<html>
<head>
<style type="text/css">
      option.selected{
            COLOR:'green';
      }
      option.notselected{
            COLOR:'red';
      }
</style>
<script language='JavaScript' type='text/javascript'>
  function ChangeClass(id) {
    var item = document.getElementById( id );
    var opt = item.options;
    for ( var i = 0; i < item.length; i++ ) {
      if ( item[ i ].selected ) {
        item[i].className = 'selected';
      }
      else{
        item[i].className = 'notselected';
      }
    }
  }
</script>
</head>
<body>
<select id='ListID' onchange='ChangeClass("ListID")'>
  <option class='selected'>Please pick one</option>
  <option class='notselected'>one</option>
  <option class='notselected'>two</option>
  <option class='notselected'>three</option>
</select>
</body>
</html>

Hope this is what you are looking for.

Night
0
 
LVL 1

Accepted Solution

by:
Nightfalcon earned 1200 total points
ID: 16698761
erratum on previous entry (changing classes of previous and next sibling)

<script language='JavaScript' type='text/javascript'>
  function ChangeClass(id) {
    var item = document.getElementById( id );
    var opt = item.options;
    for ( var i = 0; i < item.length; i++ ) {
      if ( item[ i ].selected ) {
        item[i].className = 'selected';
        if (i > 0){
          item[i-1].className = 'selected';
        }
        if (i < (item.length - 1)) {
          item[i+1].className = 'selected';
          i+=1;
        }
      }
      else{
        item[i].className = 'notselected';
      }
    }
  }
</script>
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question