![]() ![]() To only traverse a single level down the DOM tree (to return direct children), use the. You mentioned you thought this would get all children with the name frmsave inside the form this would only happen if there was a space or other combinator between the form and the selector, eg: ('form name'frmSave'') ('formname'frmSave'') literally means find all forms with the name frmSave, because there is no combinator involved. The DOM tree: This method traverse downwards along descendants of DOM elements, all the way down to the last descendant. A descendant is a child, grandchild, great-grandchild, and so on. Also there is a big difference between (this) and this. ![]() Using eq() is a little more explicit making this point than the answers using map, though map or each is what you'd probably use "in real life" ( jquery docs for eq here). The find () method returns descendant elements of the selected element. Well, if you really had wanted to use a jQuery function for some reason, you could have used (this).prop ('tagName') - but it's easier to just use this.tagName as ssilas777 said. 5 Answers Sorted by: 25 Use ('form :input') Per the docs: Description: Selects all input, textarea, select and button elements. If you want a jQuery approach, you may use: let elementName ('elementid').attr ('name') You can find more information about jQuery selectors here. I want to be overly clear that you have four items that matched that selector, so you need to deal with each explicitly. element.name seems to work as well now at least in the latest version of Chrome. Even the browser will get bored and offer you the option to kill the webpage for taking too long!Īs I said, jQuery is a powerful tool, but it should not be considered the answer to everything.With the code in the question, you're only directly interacting with the first of the four entries returned by that selector. The jQuery version might take 30 seconds to a whole minute! That's huge! People aren't going to sit around for that. Perhaps for something particularly advanced, the optimal "pure JavaScript" solution would take one second to run. Now, that's just for one operation, over time you will have more and more stuff going on in your code. Then you need to use 'id' as conceptually it is used. var elements document.getElementsB圜lassName ('className') var requiredElement elements 0 Else, if you really want to select only one element. 9 Answers Sorted by: 23 It should be known that the only correct answers that have been given are the ones that included quotes around the attribute value, ie. The "plain JavaScript" vesion is over 35 times faster than the jQuery version. If you want only the first element in the DOM with that class, you can select the first element out of the array-like HTMLCollection returned. You can get the name value from an input field using name element in jQuery by: var firstname jQuery('form1 inputnamefirstname'). JavaScript: 11,000k operations / second. ![]() First, the code given to you in the Accepted Answer: $("#ID").attr("name") Īnd second, the Vanilla JS version of it: document.getElementById('ID').getAttribute("name") With that in mind, here are two pieces of code. Judging by the current accepted answer, I am going to assume that you were able to add an ID attribute to your element and use that to select it. Syntax: selector document. The JavaScript getElementsByName() method can be used to select the required element and this can be passed to a jQuery function to use it further as a jQuery object. While there is no denying that jQuery is a powerful tool, it is a really bad idea to use it for such a trivial operation as "get an element's attribute value". Method 2: Using JavaScript to get the element by name and pass it on to jQuery.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |