Q
How can you add a CSS class to an HTML element using JavaScript?

Answer & Solution

Answer: Option A
Solution:
To add a CSS class to an HTML element using JavaScript, you can use the element.classList.add('className') method. This method adds the specified CSS class ('className') to the element, allowing you to dynamically apply styles or behavior defined in the CSS class to the element in the document.
Related Questions on Average

How can you enable a disabled HTML button using JavaScript?

A). element.disabled = false;

B). element.removeAttribute('disabled');

C). element.enable();

D). element.enable = true;

How can you set the text alignment of an HTML element using JavaScript?

A). element.style.textAlign = 'alignment';

B). element.style.align = 'alignment';

C). element.textAlign = 'alignment';

D). element.align = 'alignment';

How can you set the font color of an HTML element using JavaScript?

A). element.style.color = 'color';

B). element.style.fontColor = 'color';

C). element.fontColor = 'color';

D). element.color = 'color';

How can you show a hidden HTML element using JavaScript?

A). element.style.display = 'block';

B). element.style.visibility = 'visible';

C). element.visible = true;

D). element.style.opacity = 1;

How can you add an attribute to an HTML element using JavaScript?

A). element.setAttribute('attributeName', 'value');

B). element.attributeName = 'value';

C). element.attributeName('value');

D). element.attributeName.add('value');

How can you remove a CSS class from an HTML element using JavaScript?

A). element.classList.remove('className');

B). element.className = '';

C). element.setAttribute('class', '');

D). element.removeClass('className');

How can you set the border width of an HTML element using JavaScript?

A). element.style.borderWidth = 'width';

B). element.style.border.width = 'width';

C). element.border.width = 'width';

D). element.width = 'width';

How can you hide an HTML element using JavaScript?

A). element.style.display = 'none';

B). element.style.visibility = 'hidden';

C). element.visible = false;

D). element.style.opacity = 0;

Which JavaScript method allows you to change the value of an HTML attribute?

A). setAttribute()

B). getAttribute()

C). removeAttribute()

D). createElement()

How can you dynamically change the value of an input element using JavaScript?

A). element.value = 'New Value';

B). element.setAttribute('value', 'New Value');

C). element.setValue('New Value');

D). element.changeValue('New Value');