html javascript get length name in div
NickName:Korty Ask DateTime:2018-05-30T16:54:13

html javascript get length name in div

I need a return count of elements for example name "A"

<div id="box">
  <input name="A" value="123">
  <input name="A" value="456">
</div>

<script type="text/javascript">
  var ii = document.getElementById("box");
  var hh = ii.getElementsByName('A');
  alert(hh.length); // should return 2 but return TypeError
</script>

result: TypeError: ii.getElementsByName is not a function

Thanks for helping!!

Copyright Notice:Content Author:「Korty」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/50600399/html-javascript-get-length-name-in-div

Answers
Ankit Agarwal 2018-05-30T08:56:22

You need to use querySelectorAll('[name=A]') as getElementsByName() is not a function. And see that, it exactly gives the count of A inside of the div with id box since we use ii.querySelectorAll which selects element from the reference of ii.\n\n\r\n\r\nvar ii = document.getElementById(\"box\");\r\nvar hh = ii.querySelectorAll('[name=A]')\r\nalert(hh.length);\r\n<div id=\"box\">\r\n <input name=\"A\" value=\"123\">\r\n <input name=\"A\" value=\"456\">\r\n</div>\r\n<div id=\"box2\">\r\n <input name=\"A\" value=\"123\">\r\n <input name=\"A\" value=\"456\">\r\n</div>",


Mamun 2018-05-30T08:56:40

You can simply use querySelectorAll():\n\n\r\n\r\nvar ii = document.querySelectorAll(\"#box > input[name='A']\");\r\nalert(ii.length); // 2\r\n<div id=\"box\">\r\n <input name=\"A\" value=\"123\">\r\n <input name=\"A\" value=\"456\">\r\n</div>",


Hearner 2018-05-30T08:58:36

Use querySelectorAll\n\n\r\n\r\nconsole.log(document.querySelectorAll('#box [name*=\"A\"]').length);\r\n<div id=\"box\">\r\n <input name=\"A\" value=\"123\">\r\n <input name=\"A\" value=\"456\">\r\n</div>",


Nikhil Aggarwal 2018-05-30T08:58:48

You can use querySelectorAll and pass the hierarchy selector\n\n\r\n\r\n var hh = document.querySelectorAll(\"#box [name='A']\");\r\n console.log(hh.length); // logs 2\r\n<div id=\"box\">\r\n <input name=\"A\" value=\"123\">\r\n <input name=\"A\" value=\"456\">\r\n</div>",


MerlinMa 2018-05-30T08:56:36

getElementsByName is document's function.\n\nvar ii = document.getElementById(\"box\");\nvar hh = document.getElementsByName('A');\nalert(hh.length); \n\n\nor use jquery:\n\n$(\"#box input[name='A']\").length;",


More about “html javascript get length name in div” related questions

html javascript get length name in div

I need a return count of elements for example name "A" &lt;div id="box"&gt; &lt;input name="A" value="123"&gt; &lt;input name="A" value="456"&gt;

Show Detail

Javascript display name of div

I have a container with divs inside, using Javascript I am trying to open an alert box for each item found displaying the 'name' of the div... items = document.getElementsByClassName("single_i...

Show Detail

How to get alerts for inputed name length of characters - javascript

Hello im trying to do a simple script here when i enter name in input field, i want to get certain alerts for example: - if name is > 20 characters alert = "name is bigger than 20" - if name is b...

Show Detail

How to get the div name in javaScript?

I want to get the div name at alert(name), but it gives as "undifined". The values come from a database. All other alerts are working fine. Here is my HTML code. &lt;input class="rating form-cont...

Show Detail

How to get class name of parent div of html link from which javascript function is called?

How can I get the class name of the parent div from which a html link calls a javascript function? In the following code example: &lt;div class="parent-class"&gt; &lt;a href="javascript:parent(...

Show Detail

Get the name of HTML-Special-Characters in JavaScript

Is it possible to get the name of HTML-Special-Characters programmatically in JavaScript? Per example something like here: function HtmlEncode(s) { var el = document.createElement("div"); el.

Show Detail

HTML Length of a DIV is not equal to the AJAX Loaded HTML Length

Here is the HTML: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Test&lt;/title&gt; &lt;script src="js/jquery.js"&gt;&lt;/script&gt; &

Show Detail

javascript get html or text from div

This is easy in jquery but how about plain javascript? The code is for the Google image search API. I want to search images that match the html or preferably text inside a div. var searchThis =

Show Detail

Javascript Set div name value?

I have made this random message displayer here: &lt;div class="MessageBar"&gt; &lt;div id="Messages" name="Item"&gt; &lt;div class="style9"&gt; Mes

Show Detail

Get name from option HTML with Javascript

&lt;option value="1" name="13.890000"&gt;Monster&lt;/option&gt; I would like to get name via javascript to do calculations (On change) (NOTE: NOT VALUE) $("#calculateprice"

Show Detail