简体   繁体   中英

Add JQuery variable to $('.class').html(“HTML”);

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.

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

$('.post' + id).html(...)

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

i think this is the jQuery selector

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM