Roshan Wijesena
Roshan Wijesena

Reputation: 3136

jquery object to the string

$("h2.trigger").click(function(){
            console.log($(this));

            $(this).toggleClass("active").next().slideToggle("slow");
            var viewName=$(this).attr('id');

            //display approval list ajax method
            var html="";

I'm currently working on a jQuery application and do a console.log($(this));

In Firefox it prints

[h2#vw_hs_hr_wf_admin_data.trigger] this is a object

We can not take it as a plain string as it returns Object object. I would like to assign the "h2#vw_hs_hr_wf_admin_data.trigger" as a plain text to a JavaScript variable.

I tried

alert($(this).id); 
alert($(this).0); 

but I have no luck.

Upvotes: 0

Views: 728

Answers (2)

Jesper Haug Karsrud
Jesper Haug Karsrud

Reputation: 1193

I don't know what you'd want with this, but this thing makes concatenates the objects properties into what you want. The reason you see it like that in Firefox, is that it does that concatenation internally when presented to the user. Markup in my example is this;

<h2 id="vw_hs_hr_wf_admin_data" class="trigger">Click me</h2>
<p></p>

And then the script does its thing;

(function($) {
    $(function() {
        $('h2.trigger').click(function() {
            var text = getTagNameIdClass(this);
            alert(text);
        });
    });
    function getTagNameIdClass(obj) {
        return (obj.tagName).toLowerCase() + "#" + obj.id + "." + obj.className;
    }
})(jQuery);

Hopefully this helps? :)

Upvotes: 1

roberkules
roberkules

Reputation: 6605

just use the attr method: $(this).attr("id") ?

Upvotes: 2

Related Questions