[RESOLVED]I have done the following with Javascript. But how do I do it with Jquery?

I have the following JavaScript function that works.  How can I set the variable using Jquery instead and get it to work.  

var elemRef = document.getElementById('<%=div1.ClientID %>');

if (elemRef.offsetParent) { //alert('passed elemRef.offsetParent'); do { posX += elemRef.offsetLeft; posY += elemRef.offsetTop; } while (elemRef = elemRef.offsetParent); }

I would like to do the following instead but it doesn’t work. I don’t get any errors, it just doesn’t work.

var elemRef = $('input[id$=div1]');
if (elemRef.offsetParent)
	            {	//alert('passed elemRef.offsetParent');
	                do
	                {
	                    posX += elemRef.offsetLeft; posY += elemRef.offsetTop;
	                }
	                while (elemRef = elemRef.offsetParent);
	            }
var elemRef = $('#<%=div1.ClientID %>');

Thanks.

In what I am trying to do, I need to feed the control such as div1 in as a variable.  I use the following consistently, but am not having success with offsetParent, offsetLeft, etc. in using this notation.

var ctrl = div1;

var elemRef = $(‘input[id$=’
+ ctrl +
‘]’);

What’s wrong with using document.getElementById if it works?

If the control doesn’t exist on the page, you will get an error.  And I feed the name of the control in as a variable to a script that runs on a master page. The content pages call it.

so do null checking before using it

var elemRef = document.getElementById('<%=div1.ClientID %>');

if (!elemRef)
{
    return;
}

But you get an error if div1 isn’t on the page you are referencing it with:

var elemRef = document.getElementById(‘<%=div1.ClientID %>’);

Then you need to find it using FindControl

http://forums.asp.net/t/1963903.aspx?Finding+controls

I am finding the control using my notation, but since it is with JQuery notation instead of JavaScript, it has a problem with offsetParent, offsetLeft, and offsetTop. 

I have offsetLeft and offsetTop working now, but still have a problem with offsetParent.

Probably because when you use $() you are getting a jQuery object that wraps the actual element, but if you use document.getElementById you only get the raw element.  Try using .get on your jQuery version to get the raw element and you should have your desired
properties on that object.

http://api.jquery.com/get/

How can I use the get to find a control by name. It finds it by an index (integer) it looks like?

If I can figure out how to do this, I’ll have it.

var elemRef = $('input[id$=div1]');
if (elemRef.offsetParent) { //alert('passed elemRef.offsetParent'); do { posX += elemRef.offsetLeft; posY += elemRef.offsetTop; } while (elemRef = elemRef.offsetParent); }

The only thing I haven’t figured out now is how to deal with offsetParent

The following changes work for offsetLeft and offsetTop

if (elemRef.offsetParent)
	            {	//alert('passed elemRef.offsetParent');
	                do
	                {
	                    var offset = elemRef.offset();
	                    posX += offset.left;
	                    posY += offset.top;
	                    alert(posX + ';' + posY);
	                }
	                while (elemRef = elemRef.offsetParent);
	            }
var elemRef = $('input[id$=' + ctrl + ']').get(0);

I can’t thank you enough!  Your a genius!

Leave a Reply