last day (14 days later) » 

4:40 AM
0
A: How to get type of parent by jquery

guest271314 How can I get the whether the parent is form or table or a div etc? Try utilizing event.preventDefault() , .parents() with selector "form, table" , Node.nodeName $(".button").click(function(e) { e.preventDefault(); var p = $(this).parents("form, table")[0].nodeName; // `TABLE` or ...

 
It is returning TD for table, it should be TABLE, not the TD
 
@AbdullahMamun-Ur-Rashid "It is returning TD for table" Can create stacksnippets , jsfiddle jsfiddle.net to demonstrate ?
 
@AbdullahMamun-Ur-Rashid ? Did not compose js at jsfiddle.net/hardikvd/jxz8jkjr here . Tried stacksnippets ? See jsfiddle jsfiddle.net/jxz8jkjr/1 same js at above
 
Yes, your snippets works and it is returning the table. But what if the node is neither table or form, rather it is div or p? I can add div or p to your snippet and it will return the correct node name. But in that case I have to add all the html tag names separated by comma, It will be better if I don't have to mention tag name there .parents("form, table, div")[0], rather it will be dynamic.
 
4:40 AM
@AbdullahMamun-Ur-Rashid See at OP "How can I get the whether the parent is form or table or a div etc?" html at original post not appear to contain div or p elements having .button child nodes ? See stackoverflow.com/help/how-to-ask
 
Sorry I did not mention. I am updating the question.
Hello
 
@Abdullah Hi, "Sorry I did not mention. I am updating the question." Perhaps ask new Question including details not included at OP ? See stackoverflow.com/help/how-to-ask
 
I have updated the question
Don't need to create a new question. If you solve this, I will accept your answer in this post
If i add parents("form, table, div")[0], it can return div, but I have to add all html tag names there to return all tag type, such as p for paragraph
Can it be dynamic, so that I don't have to write down tag list?
 
Try adding a unique class to all .button parents ; e.g., form ,div , p etc., could each have class .button-parent ; could then select $(this).parents(".button-parent")[0].nodeName
 
i updated my answer with a suggestion to alter the DOM
btw have you tried anything at all?
i mean is the goal here just to know where the button is semantically when one is clicked? or to handle the different situations? in either case, my answer will give you what you need.
 
4:55 AM
Look
If a form is like this, <form><table><tr><td>Name</td><td><input type="text" name="fullname" value="" /></td></tr></table><input type="submit" value="Submit" /></form>, then it is returning 'TD' as parent
 
which was the same info as my answer. try it out
 

last day (14 days later) »