How to select containing div in mouse handler in designmode iframe

If the user clicks with a given class, or within a child node of a div with that class, I need to be able to select the div (not the child, the div of the specified class). For example, given the following html:

<div class='itemsection' id='1001'>
      text 1
      <span class='b'  id='1002'>
            text 2
      </span>
      <span class='c'  id='1003'>
            text 3
      </span>
</div>

<div class='c'>
      other text
</div>er text
</div>

If the user clicks in div 1001, 1002, or 1003, then I want to select div 1001.

I can already determine if the click is with the itemsection class using the mouse event handler in the attached code. So my question is, how do I select the div of class 'itemsection'


function handleMouseClick(e) {
        if (isInItemSection(e.target)) {
		// click is within 'itemsection' div or its child, but how do I select the itemsection div ?
        }
    }

    function isInItemSection(c) {
        do {
            try {
                if (c.className.indexOf('itemsection') != -1) {
                    return true;
                }
            } catch (e) {}
            c = c.parentNode;
        } while (c && c != null);
        return false;
    }

Open in new window

emsttamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aboo_sCommented:
function findParentDiv(elem){
var parent = elem.parentNode;
if(parent && parent.tagName.toUpperCase()!="DIV"){
parent = findParentDiv(parent);
}
return parent;
}
0
emsttamAuthor Commented:
aboo_s,

I already have code to get the div (attached to the original question), what I need to know is how to select it.
0
Albert Van HalenAnalyst developerCommented:
What do you mean with 'select' it.
Change the look of the element?
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

emsttamAuthor Commented:
This is in an iframe in designmode. Select means to emulate a user selection. It will involve setting the correct range :

http://www.quirksmode.org/dom/range_intro.html

But I don't know how to set the range for a given div.
0
Albert Van HalenAnalyst developerCommented:
Tested in FireFox.
Getting the parent div only returns true or false in your case...
Small modification is made to return the actual element...
function handleMouseClick(e) {
	var p = isInItemSection(e.target);
	if (p != null) {
		var userSelection = window.getSelection();
		var range = document.createRange();
		range.selectNode(p);
		userSelection.addRange(range);
	}
	e.cancelBubble = true;
}

function isInItemSection(c) {
	do {
		try {
			if (c.className.indexOf('itemsection') != -1) {
				return c;
			}
		} catch (e) {return null; }
		c = c.parentNode;
	} while (c && c != null);

	return null;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
emsttamAuthor Commented:
Works for me, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Languages and Standards

From novice to tech pro — start learning today.