8

Add a class with numbers to each element li, jquery

 4 years ago
source link: https://www.codesd.com/item/add-a-class-with-numbers-to-each-element-li-jquery.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Add a class with numbers to each element li, jquery

advertisements

Add class with numbers to each li elements, jquery.

Here is a sample of my code:

$('li').each(function(i) {

$(this).addClass(i);

});

There is a way to add class only with numbers, i know i am able to add class with this way $(this).addClass('something'+i); but instead i want only with numbers like:

<li class='1'></li>

Thank you!


Instead od $(this).addClass(i); try using $(this).addClass(i.toString());

But, having numbers as class names will get you into trouble... I just demonstrated how you can do this with jQuery.

Related Articles

When trying to add a class with a delay while in a jquery each loop

I'm trying to get a few elements to fade in one at a time with Javascript/jQuery and I simply can't get anything working. I've tried every 'solution' on StackOverflow there is to this problem, and nothing works. Either 1) The elements I'm trying to a

How do I add a class to the first PHP element on WordPress?

I've created a custom post type that utilizes Twitter Bootstraps carousel. I did some modification and can't seem to add the class 'active' to the first element. The place I'm trying to modify is on line 53 Pastebin code EDIT: So I'm following this t

How do I add a class to the first html element in my case?

I am using angular framework for my app. I wanted to add a class to only the first element of my ng-repeat. <div class='onlyFirst' ng-repeat = 'task in tasks'>{{task.chore}}</div> However, the above code will apply to all my div and I only nee

How to add a class to the first child elements of each set using jQuery

I wish to add a class to the first element in each set of elements. But my JS is applying to the first set only. The attempt is to add a class to the first element of a set when the document loads and removes that class on hover. HTML <div id="meg

Add a class to a dynamically generated element with css

I suspect this isn't possible, but am looking for confirmation. I have a series of image elements that are added as id's to a div. I'm trying to attach another small image to the first one. Because the container varies in width based on the user's ga

Jquery add a class and path over the elements

I have set of list elements like this <ul> <li class="first"></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li></li> <li

jQuery: Dynamically add a class that belongs to another element

I want to add a class to an element that belongs to the element that was just clicked. The class changes but they all begin with fa-. So I want to dynamically get the class of the element that was just clicked, and add it to a different element. I tr

What is the cleanest way to add a class attribute to an html element in a view in the rails

I'm writing some Rails code for a partial view, and I want it to only show a comment field if somebody is already logged onto a site here. If the page is viewed by someone who isn't a member of the site yet, the shared/comment_not_logged_in fragment

Delete the text of a p tag and add a class with jquery

What I'm triying to accomplish is that everything there's a p tag with word "border" on it it will remove the text inside it (the word border) and add a class to the P. So far this works for finding the p and adding the class. How can I remove t

Generic generic icon font as an input placeholder after you add the class with jquery

I am trying to use font-awesome icons as a placeholder in search input field. Jsfiddle examples I use corresponding html entity as a placeholder, then use pseudo class to style placeholder with correct font-family (example 2 in jsfiddle): HTML: <div

Creating arrays with properties for each element in Objective-c

tell me please, how do I create an array in which each element will have a number of properties. For example: array: | |-item 1 ( property_1-"Name1", property_2-"LastName1", property_3-"Age1"); |-item 2 ( property_1-"Nam

Add a class with Jquery or Javascript to & lt; a & gt; Mark

I have the following markup: <ul class="menubar"> <li><a href="home.php" title="Home">Home</a></li> <li><a href="services.php" title="Services">Services</a>&

When loading the page, add a class to the dynamic generated element

I am trying to add a class to an element that is dynamically added to the page. I have seen a lot of solutions for on click or other events, I can't seem to find one for on page load. I need my code to fire right away(without a user triggered event)

Add div class with php to jQuery

My variable contains a color name. My example: <?php $myvar = blueColour; ?> I need to add this value to body of a html page using jQuery: <script type="text/javascript"> jQuery(body).addClass("<?php echo $myvar; ?>")

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK