How do I get a dynamic ID?

I want to access the dynamically generated id in jquery.but, but it doesn't work in jquery, but it does work in javascript.

my jquery code:

var img =  $("#MAP"+current_img_height);
 $("#map").css({'height': img.height + "px"});

      

my javascript code:

var img = document.getElementById("MAP" + current_img_height);
$("#map").css({'height': img.height + "px"});

      

what is wrong in my jquery code and how to get the dynamic id. Any help would be much appreciated

+3


source to share


3 answers


try this if you use $('#MAP')[0]

you can get OBject.



        document.getElementById('#MAP');//Return DOM Object
        var img = $('#MAP')[0]; //returns a HTML DOM Object

        var img=$('#MAP'+current_img_height)[0];

      

+1


source


img

is a jQuery object, not a dom element reference, so it has no property height

, it has a method height()

, so

$("#map").css({'height': img.height() + "px"});

      



or

$("#map").height(img.height());

      

+6


source


Like your code:

$("#MAP"+current_img_height);

      

(if current_img_height = 100)

This is the middle $("#MAP100");

* (example: <img id="MAP100">

an item with the given ID "MAP100"

If you want to get the height from an element: do this

var height =  $("#whateverID").height();

      

or

var height =  $(".whateverClass").height();

      

0


source







All Articles