jakeva
jakeva

Reputation: 2835

jquery reconstructing a selector from a string

I am taking the value of a select element and trying to modify it so that I can have access to the onscreen preview element that the select item represents. Here is the first part of the code...

$("#single_area_select").change(function(){
        var $element = '#preview_' + $("#single_area_select").val().toLowerCase();
        elementChangedOrSelected($element);
    });

And the critical part of the elementChangedOrSelected() method...

function elementChangedOrSelected(element){
    element = '$("' + element + '")';
    alert(element);
    var position = element.position();
    alert(position);

My first alert makes it look like i've got it right (ie, $("#preview_title") ), but the second alert doesn't make an appearance which tells me that the position query is failing. Can anyone see something that I can't?

Upvotes: 0

Views: 98

Answers (2)

Patricia
Patricia

Reputation: 7802

you just need to do this:

position = $(element).position();

Upvotes: 0

Ahmed Aman
Ahmed Aman

Reputation: 2393

function elementChangedOrSelected(element){
    element = $(element);
    alert(element);
    var position = element.position();
    alert("left: " + position.left + ", top: " + position.top);
}

Upvotes: 2

Related Questions