Balkan Design
Balkan Design

Reputation: 13

jquery class prefix name selection with variable

Ok i have a problem with selecting class with prefix name using variable. This is my code:

$('.product_type').on("click", function(event) {
  type_id = event.target.id;
  $("div[class*='product_']").fadeOut("fast");
  var dbg = $("div[class*='product_']") + type_id;
  dbg.fadeIn("fast");
  $('#echo').text(show_ids);
});

Problem is at var dbg = $("div[class*='product_']") + type_id; Selection is not working when I add variable + type_id...

Upvotes: 1

Views: 147

Answers (2)

Erubiel
Erubiel

Reputation: 2972

Try with this code, you were missing the var declaration and you can just append inside the $()

$('.product_type').on("click", function(event) {
    var type_id = event.target.id;
    $("div.product_" + type_id).fadeOut("fast");
    var dbg = $("div.product_" + type_id);
    dbg.fadeIn("fast");
    $('#echo').text(show_ids); // i don't know what this line makes, just copied it from your code
});

Upvotes: 0

Mahdi Aslami Khavari
Mahdi Aslami Khavari

Reputation: 1983

i think you want to use var dbg = $("div[class*='product_" + type_id + "']"); instead of var dbg = $("div[class*='product_']") + type_id;

i think your mistake is added your name after getting object

Upvotes: 2

Related Questions