1

I'm trying to isolate one element by it's class but I need part of the class name to be from a variable as every element has a unique name.

div class="post6453"
div class="post9845"
div class="post2345"

var id = $(this).attr("id"); (ID = 6453, 9845, 2345 for example)

if(data == 1){
            $('post+ID').html("Html goes here");
            }else{
            $('post+ID').html("Html goes here");
            }

I hope this makes sense. Thanks.

1

2 Answers 2

3

your variable is id (lowercase) and not ID. Even your variable concatenation is not correct. Try with

$('.post' + id).html(...)
Sign up to request clarification or add additional context in comments.

1 Comment

I'm only using ID here as an example. But this code worked, thanks
2

if element is unique you should use ID="" instead :

i think this is the jQuery selector

$(".post"+id).html("Html goes here");

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.