![]() ![]() Also there is a big difference between (this) and this. ('img').className // it contains 'class1 class2 class3' Once you get this, just split the string as usual. It contained the names of all the classes of my element separated by blank spaces. I think it would work if i'd set it up with the. 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. Then I inspected my element on the DOM explorer and I saw a very nice attribute that I could use: className. Because it's a non-standard attribute, you'll need to use jQuery's attr or the DOM getAttribute method to get it: ('name'). Where $(this) is the object that called the function, ie the input with the onclick. div elements don't have a name attribute, but most browsers will let you add one. I'd have thought that i could do this by first getting the dom element that was clicked on, ie something like this: var form = $(this).parents("form") ![]() In the confirmSubmit, i'd like to be able to dynamically get the form object (to submit it), instead of having to hardcode the form's id, or pass it as part of the call to confirmSubmit(). To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents () or the parentsUntil () method. The DOM tree: This method only traverse a single level up the DOM tree. Var text = document.I have a form where i've replaced the submit button with an input (with type=button) with an onclick which calls an existing function: The parent () method returns the direct parent element of the selected element. To get the first matching DOM element back, call get (0) ('test').get (0) On this native element, you can call id, or any other native DOM property or function. ![]() convenience method to display unprocessed html on the same page However, when you write ('something'), it returns a jQuery object that wraps the matching DOM element (s). using the $ prefix to use the "jQuery wrapped var" convention To only traverse a single level down the DOM tree (to return direct children), use the. You need to use attr () to get the id of your button because button is a jQuery object: or you can keep your selector but do not convert it to jQuery object: you just use attr (), the properties are accessed using the attr ('property name') in jquery. 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. 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. jquery get certain class name of element which has several classes assigned. I want to be overly clear that you have four items that matched that selector, so you need to deal with each explicitly. Get class name from element, and select all elements with the same class. With the code in the question, you're only directly interacting with the first of the four entries returned by that selector.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |