1

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...

4
  • 1
    Welcome to Stack Overflow! Please take the tour (you get a badge!), have a look around, and read through the help center, in particular How do I ask a good question? It's not clear what you're asking to do. Are you trying to add the contents of type_id onto the class in the selector? Or...? It's often helpful if you post a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the [<>] toolbar button; here's how to do one). Commented Aug 25, 2018 at 12:48
  • Without your HTML, and without a clear description of the problem, we are mostly unable to help you, or any future visitors, without making guesses and assumptions. Commented Aug 25, 2018 at 12:49
  • In this case, though, you're adding the string variable type_id to a jQuery object. If you want to add it to the selector, add it to the string inside of $(). Commented Aug 25, 2018 at 12:53
  • I try that but not working... var dbg = $("div[class*='product_']" + type_id); Commented Aug 25, 2018 at 12:55

2 Answers 2

2

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

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

Comments

0

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
});

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.