简体   繁体   中英

Adding class to textarea based on select (jquery to javascript)

I have a script that changes the font of a textarea when a selection is made in a select dropdown. It's written in jQuery.

var selectedScheme = '';
$('.addon-select').change(function() {
  $('.addon-custom-textarea').removeClass(selectedScheme).addClass($(this).val());
  selectedScheme = $(this).val();
});

jQuery Fiddle

I would like to re-write this using regular JavaScript. This is what I have so far, but It does not work.

if (document.getElementsByClassName('.addon-select').value == 'arial') {
     document.getElementsByClassName('.addon-custom-textarea').className += ' arial'
}

Broken Fiddle

What am I missing?

getElementsByClassName returns an array like object. So you will have to use an index to access a specific element.

Also you have the change event handler missing for vanilla javascript.

So

document.getElementsByClassName('.addon-select')

is supposed to be

document.getElementsByClassName('addon-select')[0]

 OR

document.querySelector('.addon-select')

JS

document.querySelector('.addon-select').addEventListener('change', function() {
    var textArea = document.querySelector('.addon-custom-textarea');

    textArea.className = 'addon-custom-textarea ' + this.value;
});

Check Fiddle

You missed to attach an event handler to the select . And also I added id to the select . If you want to use class, you must access to it like

document.getElementsByClassName('addon-select')[0]

because getElementsByClassName will return you a array-like object , and you need ti get the first item from it.

 document.getElementById('selectFont').addEventListener("change", function(){ let text = document.querySelector('.addon-custom-textarea'); text.className = 'addon-custom-textarea ' + this.value; }, false); 
 input, select, textarea { padding: 10px 10px; width: 200px; } /* not important */ .arial { font-family: 'Arial'; } .times { font-family: 'Times New roman'; } .courier-new { font-family: 'Courier New'; } .verdana { font-family: 'Verdana'; } 
 <select id="selectFont" class="addon-select"> <option selected disabled>Select a font...</option> <option value="arial">Arial</option> <option value="times">Times New roman</option> <option value="courier-new">Courier New</option> <option value="verdana">Verdana</option> </select> <br /> <textarea class="addon-custom-textarea" rows="5">This is some text</textarea> 

You should be using document.getElementsByClassName('class-name')[0] to target a single element.

You can use classList to add and remove classes in an event listener - see demo below and updated fiddle here :

 var selectedScheme; document.getElementsByClassName('addon-select')[0].addEventListener('change', function() { if (selectedScheme) document.getElementsByClassName('addon-custom-textarea')[0].classList.remove(selectedScheme); document.getElementsByClassName('addon-custom-textarea')[0].classList.add(this.value); selectedScheme = this.value; }); 
 input, select, textarea { padding: 10px 10px; width: 200px; } /* not important */ .arial { font-family: 'Arial'; } .times { font-family: 'Times New roman'; } .courier-new { font-family: 'Courier New'; } .verdana { font-family: 'Verdana'; } 
 <select class="addon-select"> <option selected disabled>Select a font...</option> <option value="arial">Arial</option> <option value="times">Times New roman</option> <option value="courier-new">Courier New</option> <option value="verdana">Verdana</option> </select> <br /> <textarea class="addon-custom-textarea" rows="5">This is some text</textarea> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM