Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends
I have textarea with id="txt1" within the div tag.(dont know the div id)
with textarea id i want to find the outer tag div id.
how??????
thanks friends
Posted

Hi,

Try like the following:

C#
var $div = $('#txt1').closest('div');


OR

$(this).closest("div");


Thanks
 
Share this answer
 
JavaScript
document.getElementById("txt1").parentElement.id
 
Share this answer
 
Hi....
With jQuery you can use .closest() or .parent() (though parent only looks up 1 level, whereas .closest() goes up until it find something). I've always found .closest() easier and more robust, as it will work if you change the markup (i.e. you wrap stuff in a <span> or something).

Anyways, here's the jQuery version:

Java
<input type="radio" onlick="var id = $(this).closest('table').attr('id');" />


If you don't use a library, you can do this with just JavaScript as well.

The JavaScript:

JavaScript
function findAncestorByTagName(start, tagName) {
    if (tagName.toUpperCase() === start.nodeName.toUpperCase()) {
        return start;
    }
    else if (start === document.body) {
        return false;
    }
    else {
        return findAncestorByTagName(start.parentNode, tagName);
    }
}


The onclick handler you'll have to add:

JavaScript
<input type="radio" onclick="var par = findAncestorByTagName(this, 'div'); if (par && par.id) { /* use par.id */ }" />
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900