How to add class name to all elements starting with a specific id?


Ebenezer Isaac

Before you criticize me, I know this question may be a duplicate. I have tried every other post I found on stackoverflow but failed.

I want to add a class name to all elements in my html that start with 'subclass', then a number (classid), then a random number. I don't exactly know how many elements there are since it's dynamically generated via a servlet.

Here is the code I am trying to run:

<style>
.bold {
    font-weight: bold;
}
</style>
<script>
function highlight(classid){
    alert(classid);
    $("p[id^='subclass'+classid]").addClass('bold');
    alert('hello world');
}
</script>
<p id='subclass25'>Hello World </p>

I get an alert for classid but not for "hello world". So I'm pretty sure my JQuery is wrong....

busan kavadka

You are missing the quotes, that's why the jquery selector is not being created correctly, see below

 highlight('25');
 function highlight(classid){
        alert(classid);
        $("p[id^='subclass" + classid + "']").addClass('bold');
        alert('hello world');
    }
.bold {
    font-weight: bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p id='subclass25'>Hello World </p>

Related


Add class to all elements with specific ID jQuery

Magnus Pilgard Is it possible to use .addClass for each item with a specific ID? I have 2 Gallery containers and both need to add a class. They do have the same id #galleri_container. When trying this: $('#galleri_container').addClass('active');only affects th

Add class to all elements with specific ID jQuery

Magnus Pilgard Is it possible to use .addClass for each item with a specific ID? I have 2 Gallery containers and both need to add a class. They do have the same id #galleri_container. When trying this: $('#galleri_container').addClass('active');only affects th

Add class to all elements with specific ID jQuery

Magnus Pilgard Is it possible to use .addClass for each item with a specific ID? I have 2 Gallery containers and both need to add a class. They do have the same id #galleri_container. When trying this: $('#galleri_container').addClass('active');only affects th

Add a class to all elements with a specific class

Cyprus I'm a bit new to javascript and jquery and having some trouble doing what I'm trying to do in a "nice" way. I have an HTML page like this: <div class="list-group"> <a href="#all" id="category-all" class="list-group-item active">All</a> <a href="#"

Add a class to all elements with a specific class

Cyprus I'm a bit new to javascript and jquery and having some trouble doing what I'm trying to do in a "nice" way. I have an HTML page like this: <div class="list-group"> <a href="#all" id="category-all" class="list-group-item active">All</a> <a href="#"

Add a class to all elements with a specific class

Cyprus I'm a bit new to javascript and jquery and having some trouble doing what I'm trying to do in a "nice" way. I have an HTML page like this: <div class="list-group"> <a href="#all" id="category-all" class="list-group-item active">All</a> <a href="#"

Add a class to all elements with a specific class

Cyprus I'm a bit new to javascript and jquery and having some trouble doing what I'm trying to do in a "nice" way. I have an HTML page like this: <div class="list-group"> <a href="#all" id="category-all" class="list-group-item active">All</a> <a href="#"

Add a class to all elements with a specific class

Cyprus I'm a bit new to javascript and jquery and having some trouble doing what I'm trying to do in a "nice" way. I have an HTML page like this: <div class="list-group"> <a href="#all" id="category-all" class="list-group-item active">All</a> <a href="#"

How to add css class to all elements with specific style

LINESH JOSE I want to add a CSS class to all elements with a specific style. E.g. If there are 5 elements with style " float:left ". I want to remove " float: left " and add " align-left " class. please help me Arun P Johny The way to do it is to use filter()

How to add css class to all elements with specific style

LINESH JOSE I want to add a CSS class to all elements with a specific style. E.g. If there are 5 elements with style " float:left ". I want to remove " float: left " and add " align-left " class. Please help me Arun P Johny The way to do it is to use filter()

Hide all elements with specific class and show by ID

Ty Bailey I'm using jQuery's hide()function to hide all elements of a specific class on page load . I am trying to show the element again based on its id when the link is clicked. The class has 7 elements hidden, each with a different ID. When the included lin

How to compare all array elements with a specific name?

Krina Modi I have a list of product names in an array. I want to compare each product name with a specific product name. For example, in the array, if I find the product name "Damen Basic T-Shirt", then I want to click on that product. this is my code cy.get('

How to add a specific class to a specific ID in jQuery

Ikro I try to add a specific class when the user clicks on a li with a specific id, try to remove all other classes before the li click and add a specific class to this li, for example if the user clicks in the li id 1 assigned The class is one, if the user cl

How to add a specific class to a specific ID in jQuery

Ikro I try to add a specific class when the user clicks on a li with a specific id, try to remove all other classes before the li click and add a specific class to this li, for example if the user clicks in the li id 1 assigned The class is one, if the user cl

How to get the IDs of all elements of a specific class?

Baqer Naqvi I have the following code and want to get the IDs of all elements that belong to a specific class. code <ul> <li><label class="check-lbl"><input id="1" class="translation_box" type="checkbox">اردو</label</li> <li><label class="check-lbl"><input

How to get the IDs of all elements of a specific class?

Baqer Naqvi I have the following code and want to get the IDs of all elements that belong to a specific class. code <ul> <li><label class="check-lbl"><input id="1" class="translation_box" type="checkbox">اردو</label</li> <li><label class="check-lbl"><input