jquery adding class to variable inside if, else statement [duplicate] - javascript

This question already has answers here:
How to add a class to a given element?
(28 answers)
Closed 2 years ago.
I am trying to add a class to a variable inside one of my if else statements in a jquery function. I am getting an error x.addclass is not a function. I have tried placing the variable in () and without. It makes no difference.
var ideal = document.getElementById( 'ideal_' + id )
var Search = document.getElementById('Search_' + id)
var parent = document.getElementById(+id)
if(ideal){
parent.addClass('myclass')
} else if (Search){
parent.style.background='yellow'
} else {
parent.style.background='none'
}

.addClass() is a jQuery method, you can call that on a jQuery referenced element.
Try using classList.add():
parent.classList.add('myclass');

Related

How to use a variable in a Jquery selector [duplicate]

This question already has answers here:
jQuery selectors with variables
(5 answers)
Closed 2 years ago.
I want to be able to use a variable inside a Jquery selector:
I have 100 values on screen, called myValue11, myValue21, myValue31 etc, and I want to be able to insert data into one of these values, based on its value
var myVariable;
//Checking if the values are empty, if so, set 'myVariable' to a numeric value
if ($('#myValue11').val() == "")
{
myVariable = 1;
}
else if ($('#myValue21').val() == "")
{
myVariable = 2;
}
else if ($('#myValue31').val() == "")
{
myVariable = 3;
}
//inserting into the first empty value, by using 'myVariable'
$('#myValue+myVariable+1').val("Hello World!"); //Doesn't work
The code above doesn't work, I can't seem to find a way to insert this variable into the selector, any help would be appreciated. Thank you
Try
$('#myValue'+myVariable+'1').val("Hello World!");
You needed the extra quotes to terminate the strings.

How can I check the document for an ID and change the style on a completely different class? [duplicate]

This question already has answers here:
What do querySelectorAll and getElementsBy* methods return?
(12 answers)
Closed 3 years ago.
I'd like to check the document for a specific ID. If the ID exists, I want to change the margin of a completely different class on the same page. I'm at a loss as to why it's not working. Maybe it's not even possible?
I've tried several different versions of the same type of coding below, so I'm guessing I'm way off base...
var a = document.getElementById('CommunityTabsContainer');
var b = document.getElementsByClassName('librarysearch');
if (a != null) {
b.style.marginTop = "-68px";
} else {
b;
}
You're on the right track. Assuming that CommunityTabsContainer is the id you want to scan for and librarysearch is the class you want to change; you can use document.querySelector to do what you need (documented Here). I would write something like this:
const a = document.querySelector('#CommunityTabsContainer')
const b = document.querySelector('.librarysearch');
if (a !== null) {
b.style.marginTop = '-68px'
}
Note: in your original code, getElementsByClassName returns a collection of elements, not a single element.

Use value of id to search for class [duplicate]

This question already has answers here:
How to use JavaScript variables in jQuery selectors?
(6 answers)
Closed 4 years ago.
Im trying to get the value of an html element via Id. I now want to use that value to loop through all the elements with the class from the id.val. How can I search for a variable as a class.
$(".BtnSettings").click(function () {
var Daytime = $("#Daytime").val();
//Here i dont know what to do. Instead of .Daytime I want to search for the var Daytime. Or for $("#Daytime").val() obviously, doesnt matter.
$('.Daytime').each(function () {
$(this).prop("checked", true); // Element(s) are now enabled.
});
})
you can use the attr method of jquery to look for a specific attribute just set a value. if you want to change value of said attribute the second parameter of the function will set it for you like.
Daytime.attr("class","newClass")
$("#Daytime").attr("class")
will give you the class of #Daytime

Loop through Drop Down Option and read its attribute using jquery [duplicate]

This question already has answers here:
How to get the attributes of a HTML element using JQuery?
(4 answers)
Closed 8 years ago.
I am looping through drop down option and checking attribute.
if attribute match than counter is increase. At the end i show counter as alert.
This is my code but some how its not working dont know why
var count= 0;
$('.mydropdown option').each(function () {
var level = this.attr("myattr");
if (level == "0") {
count++;
}
});
alert(count);
}
this is a plain javascript object, it does not contain a function called .attr()
Try,
var level = $(this).attr("myattr");
Just convert the this reference into a jquery object and invoke .attr() over it

How to tell if an HTML element has a particular class in JavaScript? [duplicate]

This question already has answers here:
How can I check in JavaScript if a DOM element contains a class?
(8 answers)
Check if an element contains a class in JavaScript?
(30 answers)
Closed 9 years ago.
Is there an easy way to tell if an HTML element has a specific class? For example:
var element = document.getElementById('something');
if (element.class == 'car')
Of course an element can have multiple classes, so maybe the if statement would have to be of the following form?
if (element.class.includes('car'))
var element = document.getElementById("myid");
if (element.classList.contains("myclass")) { /* do stuff */ }
Read more on element#classList here: https://developer.mozilla.org/en-US/docs/DOM/element.classList
This link also contains a polyfill for older browsers.
If using jQuery is an option you can use hasClass() see http://api.jquery.com/hasClass/
If not you can take a look at pure JS implementation - Test if an element contains a class?
For strange reasons the name of the member containing the class(es) is className, not class. Multiple class names are separated by space.
You need to use:
class = document.getElementById("{id_of_element").getAttribute("class");
then
String[] vals = class.split(" ");
var match = false;
for (i = 0; i < vals.length;i++) {
if (vals[i].equalsIgnoreCase('car') {
match = true;
break;
}
}
if (match) {
//do something
}
HTH.

Categories

Resources