0

I was wondering if someone could help me. I'm trying to add .fancy-btn to the below using JavaScript.

<div class="button transparent_2">
<a class="extra-color-3" href="#">Get a quote</a>
</div>

I want .fancy-btn class added to the div container, but cant seem to figure out how to achieve is

Thanks

Albzi
  • 14,793
  • 5
  • 39
  • 59
user3615681
  • 261
  • 1
  • 3
  • 13

3 Answers3

4

You need to use .addClass():

$(".transparent_2").addClass('fancy-btn')
Milind Anantwar
  • 77,788
  • 22
  • 86
  • 114
0

Here is a JavaScript only method:

var $a = document.getElementsByClassName("transparent_2");
//assuming there is only 1 element (or is the first)
$a[0].className += " fancy-btn";

JSFiddle

Albzi
  • 14,793
  • 5
  • 39
  • 59
0

see here: https://stackoverflow.com/a/507157/3503886

Add a space plus the name of your new class to the className property of the element. First, put an id on the element so you can easily get a reference.

<div id="div1" class="someclass">
    <img ... id="image1" name="image1" />
</div>

Then

var d = document.getElementById("div1");
d.className = d.className + " otherclass";
Community
  • 1
  • 1
mld
  • 36
  • 1
  • 1
  • 4