Text number to existing list elements using javascript loop - javascript

I wanted to text numbers to existing list elemensts. for example:
I have the elements like this. I have created diffrent list item in to my application. But I have text numbers orderly in to it.
<div class="section-01">
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
</div>
<div class="section-02">
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li"></li>
<li class="numbers-li"></li>
<li class="numbers-li"></li>
</ul>
</div>
I wanted to add numbers to all list (.numbers-li) orderly like this:
Result should be like this
<div class="section-01">
<ul class="numbers-wrap">
<li class="numbers-li">1</li>
<li class="numbers-li">2</li>
<li class="numbers-li">3</li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li">4</li>
<li class="numbers-li">5</li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li">6</li>
<li class="numbers-li">7</li>
<li class="numbers-li">8</li>
</ul>
</div>
<div class="section-02">
<ul class="numbers-wrap">
<li class="numbers-li">9</li>
<li class="numbers-li">10</li>
<li class="numbers-li">11</li>
<li class="numbers-li">12</li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li">13</li>
<li class="numbers-li">14</li>
<li class="numbers-li">15</li>
</ul>
<ul class="numbers-wrap">
<li class="numbers-li">16</li>
<li class="numbers-li">17</li>
<li class="numbers-li">18</li>
</ul>
</div>
Also please let me know how to text only odd numbers into it

try like this:
$(document).ready(function(){
$('li').each(function(index){
$(this).text(parseInt(index,10)+ 1);
})
});
note: here $(this).text(parseInt(index)+ 1) also can be used, but you should always use base value when you are using parseInt otherwise it may lead you to some undesired results.

try this
$('.numbers-li').each(function(index){
$(this).text(parseInt(index,10) + 1);
})
Update (for odd numbers. can be used to print even numbers by changing oddum = 2)
var oddnum = 1;
$('.numbers-li').each(function(index){
$(this).text(oddnum);
oddnum = oddnum + 2;
})

Related

How to make one element of a list go to the top on page load?

I need one particular element of an unordered list to appear at the top of the list on page load, but not at the top of the page.
My code below works on Firefox and Safari, but not on Chrome or Opera. I use Javascript only.
Note that if I remove location.replace('#'); , the element will jump to the top of the page on page load, which is unwanted.
location.replace('#e');
location.replace('#');
#e {
color:#fff;
background:red;
}
#list {
overflow:auto;
height:100vh;
border:1px solid #000;
}
<div>LOGO</div>
<ul id="list">
<li id="a">Link a</li>
<li id="b">Link b</li>
<li id="c">Link c</li>
<li id="d">Link d</li>
<li id="e">Link e</li>
<li id="f">Link f</li>
<li id="g">Link g</li>
<li id="h">Link h</li>
<li id="i">Link i</li>
<li id="j">Link j</li>
<li id="k">Link k</li>
<li id="l">Link l</li>
<li id="m">Link m</li>
<li id="n">Link n</li>
<li id="o">Link o</li>
<li id="p">Link p</li>
<li id="q">Link q</li>
<li id="r">Link r</li>
<li id="s">Link s</li>
<li id="t">Link t</li>
<li id="u">Link u</li>
<li id="v">Link v</li>
<li id="w">Link w</li>
<li id="x">Link x</li>
<li id="y">Link y</li>
<li id="z">Link z</li>
<li id="aa">Link aa</li>
<li id="ab">Link ab</li>
<li id="ac">Link ac</li>
<li id="ad">Link ad</li>
<li id="ae">Link ae</li>
<li id="af">Link af</li>
<li id="ag">Link ag</li>
<li id="ah">Link ah</li>
<li id="ai">Link ai</li>
<li id="aj">Link aj</li>
<li id="ak">Link ak</li>
<li id="al">Link al</li>
<li id="am">Link am</li>
<li id="an">Link an</li>
<li id="ao">Link ao</li>
<li id="ap">Link ap</li>
<li id="aq">Link aq</li>
<li id="ar">Link ar</li>
<li id="as">Link as</li>
<li id="at">Link at</li>
<li id="au">Link au</li>
<li id="av">Link av</li>
<li id="aw">Link aw</li>
<li id="ax">Link ax</li>
<li id="ay">Link ay</li>
<li id="az">Link az</li>
</ul>
Although there is probably a smoother way to do it, here is the javascript that solves my issue:
location.replace('#e');
setTimeout(function() {
location.replace('#');
}, 1);

how to hide child elements child elements in jQuery

I have a menu populating from MySQL table and PHP up to some nested sub levels.
my menu like this:
A
B
C
If click on A first time it is showing all the child elements and again I click child elements of A it displaying child elements also fine.
But the problem is when I click on the B after open all the levels items of A it shows B sub elements fine. But again if I click A it showing all the elements except child child elements also.
I used jQuery for this.
So I want to bring back to the original state? ( only expand the top child elements, not the sub child elements ),
how to do that?
//this is my jquery code for elements clickable in menu.
$(document).ready(function() {
$(".lichild").parent().hide();
$(".limain").click(function() {
$(this).children('ul').show();
$(this).siblings(".limain").children('ul').hide();
});
$(".lichild").click(function() {
$(this).children('ul').show();
$(this).siblings().children('ul').hide()
});
});
<!-- This is the html I am generating using a PHP function -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
<li class="limain">A
<ul>
<li class="lichild">A1
<ul>
<li class="lichild">a2</li>
<li class="lichild">a1
<ul>
<li class="lichild">aaaaaa</li>
<li class="lichild">abbbbbb</li>
</ul>
</li>
</ul>
</li>
<li class="lichild">A2</li>
<li class="lichild">A3</li>
<li class="lichild">A4</li>
<li class="lichild">A5</li>
</ul>
<li class="limain">B
<ul>
<li class="lichild">B1</li>
<li class="lichild">B2</li>
</ul>
</li>
<li class="limain">C
<ul>
<li class="lichild">C1</li>
<li class="lichild">C2</li>
<li class="lichild">C3</li>
<li class="lichild">A6
<ul>
<li class="lichild">A8
<ul>
<li class="lichild">A10
<ul>
<li class="lichild">A13
</li>
<li class="lichild">A14
</li>
</ul>
</li>
<li class="lichild">A11
</li>
</ul>
</li>
<li class="lichild">A9
</li>
</ul>
</li>
<li class="lichild">A7
</li>
</ul>
</li>
<li class="limain">D
<ul>
<li class="lichild">D1</li>
<li class="lichild">D2
</li>
</ul>
</li>
</ul>
Use find inside siblings and hide it.
$(".lichild").parent().hide();
$(".limain").click(function() {
$(this).children('ul').show();
$(this).siblings(".limain").find('ul').hide(); // Change in this line
});
$(".lichild").click(function() {
$(this).children('ul').show();
$(this).siblings().children('ul').hide()
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<ul>
<li class="limain">
A
<ul>
<li class="lichild">
A1
<ul>
<li class="lichild">a2</li>
<li class="lichild">
a1
<ul>
<li class="lichild">aaaaaa</li <li class="lichild">abbbbbb</li>
</ul>
</li>
</ul>
</li>
<li class="lichild">A2</li>
<li class="lichild">A3</li>
<li class="lichild">A4</li>
<li class="lichild">A5</li>
</ul>
<li class="limain">
B
<ul>
<li class="lichild">B1</li>
<li class="lichild">B2</li>
</ul>
</li>
<li class="limain">
C
<ul>
<li class="lichild">C1</li>
<li class="lichild">C2</li>
<li class="lichild">C3</li>
<li class="lichild">
A6
<ul>
<li class="lichild">
A8
<ul>
<li class="lichild">
A10
<ul>
<li class="lichild">A13
</li>
<li class="lichild">A14
</li>
</ul>
</li>
<li class="lichild">A11
</li>
</ul>
</li>
<li class="lichild">A9
</li>
</ul>
</li>
<li class="lichild">A7
</li>
</ul>
</li>
<li class="limain">
D
<ul>
<li class="lichild">D1</li>
<li class="lichild">D2
</li>
</ul>
</li>
</ul>
$(document).ready(function () {
$(".lichild").parent().hide();
$(".limain").click(function () {
$(this).children('ul').show();
$(this).siblings().find('ul').hide();
});
$(".lichild").click(function () {
$(this).children('ul').show();
$(this).siblings('li').find('ul').hide()
});
});

Combine Navigation UL jQuery Function

I am trying to write a function that will combine two children ul#level_2and append them to their parent li#level_1 within a dropdown #navigation.
I currently have the following which does the job, but it means I have to manually target each category. I'm sure there is a far shorter way to produce the same results with very little code.
$('li#furniture ul.level_2').children('li').appendTo('li#furniture ul.level_2:first');
$('li#furniture ul.level_2').children('li').not(':first').remove();
Here is a shortened version of my current html structure & below is the desired result:
Simple Current HTML
<div id="navigation">
<ul id="level_1">
<li class="level_1">
<div class="subnav_wrapper">
<ul class="level_2">
<li class="level_2">
Link A
</li>
</ul>
<ul class="level_2">
<li class="level_2">
Link B
</li>
</ul>
</div>
</li>
</ul>
</div>
Desire HTML
<div id="navigation">
<ul id="level_1">
<li class="level_1">
<ul class="level_2">
<li class="level_2">
Link A
</li>
<li class="level_2">
Link B
</li>
</ul>
</li>
</ul>
</div>
Full Detailed HTML
<div id="navigation">
<ul id="level_1">
<li class="level_1 furniture">
Furniture
<div class="subnav_wrapper">
<ul class="level_2">
<li class="level_2 sofa">
Sofa
</li>
</ul>
<ul class="level_2">
<li class="level_2 bed">
Bed
</li>
</ul>
</div>
</li>
<li class="level_1 bathroom">
Bathroom
<div class="subnav_wrapper">
<ul class="level_2">
<li class="level_2 shower">
<a href="#">Shower/a>
</li>
</ul>
<ul class="level_2">
<li class="level_2 bath">
Bath
</li>
</ul>
</div>
</li>
</ul>
</div>
This should do what you need.
var wrapper = $(".subnav_wrapper").contents();
$(".subnav_wrapper").replaceWith(wrapper );
$('ul.level_2').children('li').appendTo('ul.level_2:first');
$('ul.level_2').not(':first').remove();
JSFIDDLE DEMO

Use JavaScript to Wrap Unique Groups of Elements with DIVs

I am using a WordPress plugin that dynamically creates the following list on the fly:
<ul>
<li class="proteins"></li>
<li class="seafood"></li>
<li class="dairy"></li>
<li class="veggies"></li>
<li class="fruit"></li>
<li class="nuts-seeds"></li>
<li class="grains"></li>
<li class="pasta"></li>
<li class="spices"></li>
<li class="herbs"></li>
<li class="technique"></li>
<li class="special"></li>
<li class="servings"></li>
</ul>
I need to wrap specific lis in unique divs like this:
<ul>
<div id="main">
<li class="proteins"></li>
<li class="seafood"></li>
<li class="dairy"></li>
<li class="veggies"></li>
<li class="fruit"></li>
</div>
<div id="carbs">
<li class="nuts-seeds"></li>
<li class="grains"></li>
<li class="pasta"></li>
</div>
<div id="spice">
<li class="spices"></li>
<li class="herbs"></li>
</div>
<div id="other">
<li class="technique"></li>
<li class="special"></li>
<li class="servings"></li>
</div>
</ul>
I've read about using the wrapall() function but I can't find instructions on how to wrap a group of elements that do not have the same class.
Assuming that you take the time to define the relationship between the class-names and the eventual id under which they should appear, and that you use valid HTML, the following plain JavaScript works (albeit I think it could be refined):
var grouped = {
'main' : ['proteins','seafood','dairy','veggies','fruit'],
'carbs' : ['nuts-seeds','grains','pasta'],
'spice' : ['spices','herbs'],
'other' : ['technique','special','servings']
};
function groupBy(el, map) {
var els,
newList = document.createElement('ul'),
newListItem = document.createElement('li'),
tmpList, tmpLI;
for (var key in map) {
if (map.hasOwnProperty(key)){
els = el.querySelectorAll('.' + map[key].join(', .'));
tmpList = newList.cloneNode();
tmpLI = newListItem.cloneNode();
tmpLI.appendChild(tmpList);
tmpLI.id = key;
for (var i = 0, len = els.length; i < len; i++){
tmpList.appendChild(els[i]);
}
el.appendChild(tmpLI);
}
}
}
groupBy(document.querySelector('ul'),grouped);
JS Fiddle demo.
The above function converts:
<ul>
<li class="proteins"></li>
<li class="seafood"></li>
<li class="dairy"></li>
<li class="veggies"></li>
<li class="fruit"></li>
<li class="nuts-seeds"></li>
<li class="grains"></li>
<li class="pasta"></li>
<li class="spices"></li>
<li class="herbs"></li>
<li class="technique"></li>
<li class="special"></li>
<li class="servings"></li>
</ul>
Into:
<ul>
<li id="main">
<ul>
<li class="proteins"></li>
<li class="seafood"></li>
<li class="dairy"></li>
<li class="veggies"></li>
<li class="fruit"></li>
</ul>
</li>
<li id="carbs">
<ul>
<li class="nuts-seeds"></li>
<li class="grains"></li>
<li class="pasta"></li>
</ul>
</li>
<li id="spice">
<ul>
<li class="spices"></li>
<li class="herbs"></li>
</ul>
</li>
<li id="other">
<ul>
<li class="technique"></li>
<li class="special"></li>
<li class="servings"></li>
</ul>
</li>
</ul>

Variable width for second level ul in Nicemenu (Drupal)

I have a menu structure like the below in Drupal 7. This is a part of the Nicemenu module in Drupal. I am applying a width to the second level ul - the sub menu ul, and this gets applied to all the second level ul. What I need is a variable width for the second level ul's. All these HTML sre dynamically generated by Drupal and I may not be able to hard code any classes or ID to these second level uls. I targeted these using pseudo-classes but didn't achieve what I want.
Here's the HTML
<ul class="menu">
<li class="first leaf"><a class="active" href="">Menu 1</a></li>
<li class="expanded"><a title="" href="">Menu 2</a>
<ul class="menu"> <!--This should have width of 200px-->
<li class="first leaf">Menu 2a</li>
<li class="last leaf">Menu 2b</li>
</ul>
</li>
<li class="expanded"><a title="" href="">Menu 3</a>
<ul class="menu"><!-- Width should be 350px-->
<li class="first leaf">Menu 3a</li>
<li class="leaf">Menu 3b</li>
<li class="leaf">Menu 3c</li>
<li class="leaf">Menu 3d</li>
<li class="leaf">Menu 3e</li>
<li class="leaf">Menu 3f</li>
<li class="leaf">Menu 3g</li>
<li class="last leaf">Menu 3h</li>
</ul>
</li>
<li class="expanded"><a title="" href="">Menu 4</a>
<ul class="menu"><!-- Width should be 200px-->
<li class="first expanded"><a title="" href="">Menu 4a</a><ul class="menu">
<li class="first last leaf">Menu 4a-1</li>
</ul>
</li>
<li class="last leaf">Menu 4b</li>
</ul>
</li>
<li class="leaf">Menu 5</li>
<li class="expanded"><a title="" href="">Menu 5a</a>
<ul class="menu"><!-- Width should be 150px-->
<li class="first last leaf">Menu 5b
</li>
</ul>
</li>
<li class="leaf">Menu 6</li>
<li class="leaf">Menu 7</li>
<li class="expanded"><a title="" href="">Menu 8</a>
<ul class="menu"><!-- Width should be 200px-->
<li class="first leaf">Menu 8</li>
<li class="last leaf">Menu 9</li>
</ul></li>
<li class="last leaf">Contact Us</li>
</ul>
You really need id attributes in your <ul> elements, so you can use CSS to set the widths for each id. This is the most convenient and robust way to style separate HTML elements.
Add html ID to content field shows a solution how to add these id attributes.

Categories