views:

125

answers:

6

How could I change the text below so that the text within it has a number appended to it.

<div class="right">This is some text</div>
<div class="right">This is some text</div>
<div class="right">This is some text</div>

So the code above would become,

  1. This is some text
  2. This is some text
  3. This is some text
+5  A: 

you should use an ordered list... ol

or else you will need use css and add the content property your selector with the :after pseudo element.

Thorn007
How would I apply the :after pseudo element to cycle through all the elements. Looked at the documentment, it just checks the next one doesn't it?
usertest
oops sorry its :before in your case...see http://www.w3.org/TR/CSS21/syndata.html#counter
Thorn007
+1 for recommanding this
marcgg
A: 

Does this have to be done dynamically through jquery? Can't you just combine all that text into one div and then make a ordered list around it?

chobo2
The classes will move around dynamically, so they need to be renumbered dynamically.
usertest
+1  A: 

jQuery selectors are your friend... Get your stuff and loop on through something like this:

texts = $("div.right");
for(i = 0;i < texts.length;i++)
{
     node = $(texts[i]);
     content = node.html();
     number = i + 1;
     node.html(number + ". " + content);
}

Update: Jeez, last time post untested code straight off the dome here (disclaimer: not actually the last time). In the interest of correctness, I've updated it to at least run (and work!) if you still want to do it this way. Although I admit the other solutions are cleaner and more elegant.

pivotal
Don't forget a space: texts[i].html( i + ". " + content);Otherwise, that's perfect.
Frank DeRosa
It didn't work and Firebug says "texts[i].html is not a function"
usertest
My mistake, that will give you a bad array index... I've updated it to work correctly.
pivotal
you should not access arrays with [] because it will most likely break in IE 6 (and maybe 7). There is an accessor that I forgot...
marcgg
texts[i] will return a DOM element - not a jQuery object. You need to use texts.eq(i) in your example.
David Murdoch
+1  A: 

This is really an elaboration on another comment. I can't format code in a comment, I guess. You could use jQuery core's each:

$('div.right').each(function(ii){
     html = $(this).html();
     $(this).html(ii + '. ' + html);
});
mqsoh
It would number them 0, 1, 2 instead of 1, 2, 3.
David Murdoch
+3  A: 

How about the following?

$("div.right").each(function(i){
    $(this).prepend((i + 1) + ". ");
});

UPDATE:

Here is one way that should work.

"number" is a custom element (it can be anything you want) that will/should be ignored by browsers.

$("div.right").each(function(i){
    $(this).find("number").remove().end()
           .prepend("<number>(i + 1) + ". </number>");
});

OR use the following which is probably a little slower but semantically correct...

$("div.right").each(function(i){
    $(this).find("span.number").remove().end()
           .prepend("<span class='number'>" + (i + 1) + ". </span>");
});

OR an even better way would be to prepend span.number before your first drag:

$(function(){ // document ready...
   // caching the "numbers" will only work if you use the DOM
   // for updating div position (like jQuery's "append()", "prepend()", "before()", and "after()") and not "innerHTML" or "html()"
   var numbers = $("div.right").each(function(i){
        $(this).prepend("<span class='number'>" + (++i) + "</span>. ");
    }).find("span.number");

    function dragEnd(){
        // do your drag end stuff here...
        numbers.each(function(i){
            this.innerHTML = ++i;
        });
    )};
});
David Murdoch
+1- In my opinion, this is probably the best way
Russ Cam
Excellent, but one question. Everytime the divs are reordered the a new number is added instead of replacing the existing one. How to get around this?
usertest
didn't know about the re-ordering. Do you HAVE to keep them in divs? Can you use an ordered list?
David Murdoch
No, the layouts too complex
usertest
I gave the number a class and just removed it the everytime the layout was updated. Thanks.
usertest
Your welcome. Try out the last method in my answer if speed is an issue. It should be faster.
David Murdoch
A: 

Using [] notation with a result set will give you the raw DOM element which does not have the html() function. Use the eq() function to get each element wrapped in a jQuery object.

You can also use each() as mentioned above, but I prefer straight 'for loops' so I don't have to adjust for 'this' if I'm in an event handler.

var texts = $("div.right");
var elem;
for(i = 1; i < texts.length; i++) {
    elem = texts.eq(i);
    html = elem.html();
    elem.html(i + '. ' + html);
}
jshalvi