Reputation: 47783
I want to add a class to my
<p class="formField" id="equipmentTypeContent">some content is here...</p>
Here is how I tried to do that but it's not working:
$('#equipmentTypeContent').addClass("singleEquipmentText");
I don't know if in this case jQuery needs to know what type of element it is (in this case a
tag) in addition to the type of tag? I doubt it, I think it only needs the id of the element you're manipulating right?
Upvotes: 0
Views: 74
Reputation: 7954
Nothing wrong with your code try with with jquery ready function if the script is above your HTML
$(document).ready(function(){
$('#equipmentTypeContent').addClass("singleEquipmentText");
});
YOu can access using the class too if you want to add to all of them with that class
$(document).ready(function(){
$('.formField').addClass("singleEquipmentText");
});
Upvotes: 0
Reputation: 17071
Here is a fiddle showing how to do it (and proving it by returning the updated class of the p element) after a selection is changed in a dropdown.
I'm using the jQuery .change()
function to execute certain code after the value of the select element is changed. In this case, I take the value of the select element, strip the spaces, and add the resulting, space-free string as a class to the p element.
Upvotes: 1
Reputation: 39501
There's nothing wrong with your code. See the working copy on jsFiddle
Upvotes: 1
Reputation: 382861
Your code seems fine but you need to wrap your code in ready handler:
$(function(){
$('#equipmentTypeContent').addClass("singleEquipmentText");
});
Also make sure that you have loaded jQuery on your page.
Upvotes: 2