1

How to get number of HTML elements (HTML tags) in a string?

For example I have this string:

<div> some text
     <div>
           <label>some text</label>
     </div>
</div>

Now how would I find number of html tags in this string? The result in the case above will be 3.

I did tried this, thought it will work but it didn't:

$('*').length;
3
  • Check below code is helpful or not.. Commented Jan 23, 2013 at 5:06
  • I am confused. Why a string? Couldn't you just traverse the DOM with .each() and have an incrimentor output your total? Commented Jan 23, 2013 at 5:06
  • am giving a try to each of the answer @Sahal Commented Jan 23, 2013 at 5:15

6 Answers 6

4
var s = "<div> some text<div><label>some text</label></div></div>";   
var length = $(s).find('*').andSelf().length;

http://jsfiddle.net/jCW7Z/

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

8 Comments

Why do you need the .andSelf()? It's odd that the outer <div> doesn't get matched.
@Blender Do you mean andSelf is redundant? The first div is wrapper element, it has 2 descendant elements + 1 === 3?
For some reason I thought jQuery would create another outer wrapper element to wrap everything. It seems like all top-level elements will become wrapper elements: $('<div><b></b></div><div><b></b></div>').find('*').length === 2
@Blender Yes, that's true, all top-level elements become wrapper elements.
@undefined can we get this totall length without counting these tags <b>,<i>,<span> ?
|
3

If you're using jQuery (and it looks like you are), the easiest way would be to use code similar to the following:

var count = $( yourString ).find('*').andSelf().length

However, if you're using vanilla javascript, the implementation would look more like this:

var temp = document.createElement('div');
temp.innerHTML = yourString;

var count = temp.getElementsByTagName('*').length;

Comments

1
  1. Get all elements in the document (document.getElementsByTagName('*'))
  2. Do a regular expression match on the element's className attribute for each element

You can use jQuery: $("html *") which will return all elements between the html tags

  1. for names you must use $("html *").attr('name')
  2. for values $("html *").val() or $("html *").attr('value')

Comments

0

Possible way to do it: http://api.jquery.com/length/

check out Find number of element in string with jQuery

for example

$( '.myClass', myData ).length;

(although this might not show outermost tags)

Comments

0

You can do it like this,

Put the string in to some hidden div like

$('#myHiddenDiv').html(string);

then

You can gen number of children under it

This will tell you number of html elements under the body

var count = $("#myHiddenDiv> *").length;

or:

var count = $("#myHiddenDiv").children().length;

Your div will be like this

<div id="myHiddenDiv" style="display:none"></div>

1 Comment

string will be dynamic mean, I will be getting it on click of iframe
0

A small, self documenting, example :)

<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>List Elements</title>
</head>
<body>
<div>some text
    <div>
        <label>some text</label>
    </div>
</div>
<script>
var x=document.getElementsByTagName('*');
console.log(x.length)
for (i=0;i<x.length;i++) {
    console.log(x[i]);
}
</script>
</body>

</html>

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.