0

I want to disable an element and also set its placeholder in Javascript.

I'm able to do this with the below code (I'm referring to the writecontent element).

function toggleType() {
    var type = document.querySelector('select[name="type"]').value;

    if(type == 'Review') {
        aboutreview.style.visibility = ('visible');
        document.querySelector('.writecontent').disabled = true;
        document.querySelector('.writecontent').placeholder = "Not available in Review";

    } else if (type == 'Discussion' || '') {
        aboutreview.style.visibility = ('hidden');
        document.querySelector('.writecontent').disabled = false;

Instead of calling the element twice, how would I set the disabled and placeholder at the same time when calling the element the first time?

1
  • Btw your second condition won't do what you think. You need if (type == 'Discussion' || type == '') or if ((type || 'Discussion') == 'Discussion') Commented May 4, 2020 at 16:47

1 Answer 1

1

May be by storing the element reference in a variable.

 const writeElem  = document.querySelector('.writecontent');
 if (writeElem) {
     writeElem.disabled = true;
     writeElem.placeholder = "Not available in Review";
 }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.