This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get the elements number inside parent div jquery
Is it possible to get the number of elements that are inside a div?
<div id=count">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
the resut should be
4
$('#count').children().length; // 4
var count = $('#count > *').length;
Related
This question already has answers here:
How do I change the text of a span element using JavaScript?
(18 answers)
Closed 5 years ago.
I want to know what javascript code I can use to edit the text of this word?
This is not a duplicate because this has multiple divs in a div. And the target word is located in a div that is inside the more divs.
<div id="jump">
<div class="kick">
<div class="meet">
<div class="balls">
<div class="word">
Hello
</div>
</div>
</div>
It's the 1st element that has this class...
document.getElementsByClassName('word')[0].innerHTML='Goodbye';
It's the 5th element that has this tag...
document.getElementsByTagName('div')[4].innerHTML='Goodbye';
You can use next code:
document.querySelector('.word').textContent = 'Hello World!';
<div id="jump">
<div class="kick">
<div class="meet">
<div class="balls">
<div class="word">
Hello
</div>
</div>
</div>
But if you has few elements with class word, function 'querySelectror fining only first element'.If you need get many elements, you can use document.querySelectorAll --- is HTMLCollection (not array).
I don't recommend to use getElementByClassName - is very slowly method.
Method getElementsByTagName -faster that querySelectorAll , but it use only tagName. getElementById -is the fastest menthod finding of elements.
But that method find only first element with current ID.
This question already has answers here:
Replace Div with another Div
(3 answers)
Closed 6 years ago.
I need a simple solution for replacing a div with a new div, cross-browser compatible using javascript or JQuery. I'll add some code below. Div "myDiv-B" needs to be replaced by a new div:
<div id="myDiv-C">{% include 'snippets/contactpaneel.rain' %}</div>
Here are my divs
<div id="myDiv-A">
<div id="myDiv-B"></div>
</div>
You should use .replaceWith() method.
.replaceWith method replace each element in the set of matched
elements.
$('#myDiv-B').replaceWith('<div>Hello</div>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="myDiv-A">
<div id="myDiv-B"></div>
</div>
This question already has answers here:
JQuery: How to find out how many children an element has?
(2 answers)
Closed 8 years ago.
Fiddle: http://liveweave.com/Aq56fN
In my fiddle I have a div that has a few elements as it's children. Is there anyway to detect how many children an element has as a numeric value? If so how would something like this be done?
JavaScript/JQuery:
$(".grab-contenu").on('click', function() {
$(".spectacle-contenu").html(
$(".contenu").children().toString()
);
});
HTML:
<div class="contenu" align="center">
<header class="largeur-objet">My width is 80%</header>
<button class="grab-contenu">Detect children number</button>
<footer class="spectacle-contenu">Show children number</footer>
</div>
Any help is greatly appreciated.
Try changing
$(".contenu").children().toString()
To
$(".contenu").children().length
This question already has answers here:
jQuery: How to get the value of an html attribute?
(6 answers)
Closed 8 years ago.
I have a link, how can i get the value of the alt attr in my href ? (1)
<div class="user_line">
<img class="delete_user" src="images/close_button_mini.gif">
<a class="chat_user" alt="1|Test" href="#">Test</a>
</div>
// With $this
$('.delete_user').live('click',function(){
}
Thanks
you can take the alt like this
var alt=$('.chat_user').attr('alt');
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Wrap every 3 divs in a div
jQuery - use wrap() to wrap multiple elements?
Lets say I have 4 divs, as below:
<div class="section">1</div>
<div class="section">2</div>
<div class="section">3</div>
<div class="section">4</div>
I would like to use $('.section' [increments of 2] ).wrap('<div class="row"></div>') so each 2 div.section's will be wrapped with div.row, so the end result would look like this:
<div class="row">
<div class="section">1</div>
<div class="section">2</div>
</div>
<div class="row">
<div class="section">3</div>
<div class="section">4</div>
</div>
How is this done?
$('.section:even').each(function(){
$(this).next().andSelf().wrapAll('<div class="row">');
});
Based on jQuery - use wrap() to wrap multiple elements?