2

I am trying to get this class name dynamically in jquery:

echo "<div id=\"maini\" class=\"type_" .$category. " start_" .$adSize. " color_" .$color. "\"> \n";
     echo " " . $siteName . " \n";  
echo "</div> \n";

as you can see the class name is generated with PHP.

I need the class name for this snippet of code. But what I tried does not work -> $("div." + this.id). :

    $(document).ready(function() {
        maini_s = $("div." + this.id).remove();
        num_of_arts = maini_s.length;
        ipp = 3;

Here is the entire js file in case you need to see the other elements before.

var maini_s;
var num_of_arts;
var ipp;

function handlePaginationClick(new_page_index, pagination_container) {
    var pc = $(pagination_container);
    pc.children('div#maini').remove();
    for(var i=new_page_index*ipp; i < (new_page_index+1)*ipp ;i++) {
        if (i < num_of_arts) {
                pc.append(maini_s[i]);
        }
    }
    return false;
}

$(document).ready(function() {
    maini_s = $("div." + this.id).remove();
    num_of_arts = maini_s.length;
    ipp = 3;


    // First Parameter: number of items
    // Second Parameter: options object
    $("#News-Pagination").pagination(11, {
        items_per_page:ipp, 
        callback:handlePaginationClick
    });
});

3 Answers 3

3

To get the classname, just pull the class attribute:

var className = $(".element").attr("class");

Be sure that you take into consideration that sometimes elements have multiple class names, seperated by spaces.

Sign up to request clarification or add additional context in comments.

Comments

1

I may be missing something but the syntax...

"div." + this.id

would be looking for DIVs with the class name contained in this.id, not the ID. If you want to select by ID, I believe you'd want...

"div#" + this.id

which would use the # ID selector and not the . class selector.

Comments

0

you call maini_s = $("div." + this.id).remove(); from the $(document).ready(function()

The this keyword in this case refers to the window.document element .. which normally has no id ..

are you sure you want to run it from there ? or perhaps a click event on some other item ?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.