0

I have 'span' tags and each span i need to increment 'left' property by 10px.

Ex:-

CSS:
span.cell_0     {   left:   0px;       }
span.cell_1     {   left:   10px;      }
span.cell_2     {   left:   20px;      }
HTML:
<span class="cell_0">name</span>
<span class="cell_1">#</span>

i have javascript written as:

$( 'span' ).css( 'left', '+=10px' );  

but am trying for each span

Am designing layout; rows and columns.

<div class="row1">
 <span class="cell_0">name</span>
 <span class="cell_1">#</span>
</div>
<div class="row2">
 <span class="cell_0">name</span>
 <span class="cell_1">#</span>
</div>

CSS for rows:

.row1, .row2, .row3, .row4{
font-size:12px;
padding:5px 20px 20px 0;
}
4
  • 2
    And you haven't written a single line of javascript to start with ? Commented Nov 16, 2012 at 22:37
  • Do you want to increment it on click, on hover, on load? Commented Nov 16, 2012 at 22:41
  • @bborisovs yes it is onload (kind of am designing layout) Commented Nov 16, 2012 at 22:47
  • @jeromesmadja i have written; i lost the javascript while posting. some editing issue. Commented Nov 16, 2012 at 23:02

4 Answers 4

4

Ok, I would say you should apply a class to the divs in question:

<div class="indent">
   <span class="cell_0">name</span>
   <span class="cell_1">#</span>
</div>
<div class="indent">
   <span class="cell_0">name</span>
   <span class="cell_1">#</span>
</div>

$('.indent').each(function(){
    $(this).find('span').each(function(i, v){
         $(this).css('left', (i * 10) + 'px');
    });
});

That's one way to do it. I don't know what you're doing with the classes assigned to each span. But if it is for the sake of trying to indent, you can just remove the classes from span and let the generic selector do the work for you.

Sign up to request clarification or add additional context in comments.

8 Comments

You were faster than me. but yes it is as simple as that.
that's under the assumption that he doesn't have any other spans on his page
I know. I should have voted to close but I was bored... or something. Notice I didn't bother using v... that's my revenge I guess.
@wirey - true, but he said "each span." He should just apply a single class to all that need this attention but we don't know what he's up to or what he really needs. I'm actually surprised this many of us felt the need to give this kind of attention to this thread. we're all too kind
@Kai Qing Just trying to get last upvote for the daily cap :)
|
2

Here's what I would do with what you've given

$('span.[class^="cell_"]').each(function(i,v){ // loop through each span with class that starts with cell_
   var num = +$(v).attr('class').split('_')[1]; // get the number from the class
   $(v).css('left',(num * 10) + 'px') // use it to get the px
});

1 Comment

eh.. not too worried. Just want to help :)
1

Use Attribute Starts With Selector to select only needed spans by class:

$(document).ready(function() {
    $("[class^=cell]'").each(function(i, v){
        $(this).css('left', (i * 10) + 'px');
    });      
});

Comments

0

Are you using a frame work like jquery/prototype to select the spans from the html? You can just use document.getElementsByTagName('span') then just loop them and increment the property (left)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.