3
<div id="button">
Click me
</div>

<div id=item1> //loads with a dashed border
</div>

<div id=item2> //loads with a solid border
</div>

<div id=item3> //loads with a solid border
</div>

Script part:

var eventNext = document.getElementById("button");
eventNext.addEventListener("click", move, false);

function move()
{

}

What would I put in the move function to get the next div item that does not have a dashed border, and make it dashed, and current item's border to solid? (if there is a next item)?

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
antonpug
  • 13,724
  • 28
  • 88
  • 129
  • 1
    Are you aware of the Javascript library jQuery? If not, you may want to use it. It makes it easier to do most things in Javascript, masks cross-browser incompatibilities so you don't have to write special code for them, and has a great deal of useful plugins that can save you a lot of time reinventing the wheels. Notably, [addEventListener](https://developer.mozilla.org/en/DOM/element.addEventListener) behaves a little bit different from browser to browser, whereas [the jQuery Event object](http://api.jquery.com/category/events/event-object/) is reliable across browsers. – Conspicuous Compiler Nov 28 '11 at 03:41
  • is number items 1,2,3,4 ect predefined? is there any other elements/text between the items
    between
    ?
    – david Nov 28 '11 at 04:13
  • FYI you could use the same complex selectors than jQuery with Sizzle http://sizzlejs.com/ if you don't want to use jQuery, though you'll still have the problems that @ConspicuousCompiler describes. See http://stackoverflow.com/questions/3952817/decoupling-jquery-sizzle . From a perf view, last version of CDN jQuery is probably already in the user cache, no need to use "only" Sizzle :) – FelipeAls Nov 28 '11 at 04:18

4 Answers4

3
var eventNext = document.getElementById("button");
eventNext.addEventListener("click", move, false);

function move() {
    if( eventNext ) {
        if( eventNext.id !== 'button' ) {
            eventNext.className = 'solidBorder';
        }
        eventNext = eventNext.nextElementSibling;
        if( eventNext ) {
            eventNext.className = 'dashBorder';
        }
    }
}

.dashBorder {
    border: 2px dashed blue;
}
.solidBorder{
    border: 2px solid blue;
}

or with jquery

var eventNext = $("#button").bind("click", move);

function move() {
    if( eventNext.length ) {
        if( eventNext.attr('id') !== 'button' ) {
            eventNext.attr( 'class', 'solidBorder' );
        }
        eventNext = eventNext.next();
        if( eventNext.length ) {
            eventNext.attr( 'class', 'dashBorder' );
        }
    }
}

if you need to support browsers that dont support nextElementSibling, use this functoin instead.

function next( elem ) {
    while( (elem = elem.nextSibling) && (elem.nodeType !== 1) );
    return elem;
}
shredder
  • 381
  • 2
  • 3
  • `nextElementSibling` is very much not cross-browser safe yet. – eyelidlessness Nov 28 '11 at 04:42
  • That's true, but the question uses addEventListener, and most browsers that support it also support nextElementSibling. – shredder Nov 28 '11 at 04:43
  • That's... not true. `addEventListener` was added in Firefox 1.0 (actually prior), Safari 1.0, Chrome 1.0, Opera 7, and IE 9. `nextElementSibling` was added in **HTML5** which wasn't even on the horizon when most of those were released. Notably, it wasn't added in Firefox until 3.5. I can't find data on other browsers, but the point is it's worth noting as a caveat. – eyelidlessness Nov 28 '11 at 04:52
  • @eyelidlessness - I updated my answer with a function to use in its place. But when I said "most browsers", I meant most browsers that are commonly supported. If you use as a basis the browsers that jquery supports, and limit to those which support addEventListener, then I'm pretty sure the only omission would be Firefox 3. Not sure about Opera 9. – shredder Nov 28 '11 at 04:56
2

Here's one way with jQuery, if using this library happens to be an option for you:

First, give each div which can become dashed, a "marker class"

<div id="item1" class="itemWhichCanBeDashed">
    //loads with a dashed border
</div>
<div id="item2" class="itemWhichCanBeDashed">
    //loads with a solid border
</div>
<div id="item3" class="itemWhichCanBeDashed">
    //loads with a solid border
</div>

Then create a dashed border style:

<style type="text/css">
    .dashed { border-style: dashed; }
</style>

Then, to dash the next div that's not already dashed:

$("div.itemWhichCanBeDashed:not(.dashed):first").addClass("dashed");

This selects all divs with the class itemWhichCanBeDashed, but does not have the dashed class attached, then takes the first one, then adds the class dashed

If you want the first div to already be dashed, then just render it with the dashed class.

I'm not sure exactly what the requirement of making the current div solid is, but it should be a simple extension of this.

EDIT

To host jQuery in your project, you can link to it from Google:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>

If your user has recently visited a site that was linking to the same file, it'll likely be cached. If not, it's only about a 92K download.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
1

This would be easier if you used a js framework like jquery. It's as simple as adding a reference to your head like this:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>

Using jquery, the code would be

var lastChanged;

$(document).ready(function() {
    lastChanged = $('#item1');

    $('#button').click(function() {
        $(lastChanged).css("border", "1px solid #000");
        $(lastChanged).next().css("border", "1px dashed #000");
        lastChanged = $(lastChanged).next();
    }); 
});

Here it is on jsfiddle - http://jsfiddle.net/JKYue/

Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
  • Not sure who downvoted you, but this answer looks ok. I think we're both victim of someone who hates jQuery answers, which I've actually seen before +1 from me. – Adam Rackis Nov 28 '11 at 04:15
0

see if this help to get you started

  <button onclick="nextItem()">Click me</button>


  var nextItem = (function() {

   var arr_item, arr_len, intIdx, currentItemInt;

   arr_item = ["item1", "item2", "item3"];

    arr_len = arr_item.length;
    intIdx = -1;
    currentItemInt = "";

    return function(){

       for (var j= 0; j < arr_len; j++) {

          var elm = document.getElementById(arr_item[j]);
          var os = (elm.currentStyle) ? elm.currentStyle["borderStyle"] : window.getComputedStyle(elm,"").getPropertyValue('border-top-style');

          if(os == "dashed"){
            intIdx = j;
            //alert(j)
            currentItemInt = arr_item[j + 1];
          }
   }

// alert(arr_item[intIdx]) 

   if(intIdx < arr_item.length-1){

     document.getElementById(arr_item[intIdx]).style.border = "white dashed";
     document.getElementById(currentItemInt).style.border = "black dashed";

   }
}
}());
david
  • 4,218
  • 3
  • 23
  • 25