0

I wanted the Input and Textarea to turn red through javascript when the values are empty, it worked for the input but not for the textarea. Can someone help?

$(document).on("click", '.btn-info.mailContact', function () {
    values = {
        Onderwerp: $('.Subject').val(),
        Text: $('.TheMessage').value,
    };
    if ($('.Subject').val() != "" && $('.TheMessage').val() != "") {
    State.sendContactMail(values);
    window.location.href = '/confirmation';
    } else {
        Onderwerp.style.color = Onderwerp.value === "" ? "#f00" : "#0f0";
        Onderwerp.style.borderColor = Onderwerp.value === "" ? "#f00" : "#0f0";
        Text.style.color = Text.value === "" ? "#f00" : "#0f0";
        Text.style.borderColor = Text.value === "" ? "#f00" : "#0f0";

    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" class="form-control Subject" id="Onderwerp" placeholder="Vul je onderwerp hier in"/>

<textarea class="form-control TheMessage" id="Text"  wrap="soft" placeholder="Vul je bericht hier in"></textarea>

<a id="btn-mailContact" class="btn btn-info mailContact">Verstuur contactformulier</a>
Farhad
  • 4,119
  • 8
  • 43
  • 66

3 Answers3

0

You are depending on an element with an ID getting a matching global variable.

This is a terrible idea because it makes code very confusing and hard to maintain. The variables seem to appear out of nowhere.

One of the problems (with using globals in general) is that other code might define a global with the same name.

In this case the global Text variable already has a value defined by the browser.

Don't use globals like that. You're already using jQuery, so create a jQuery object using an ID selector.

While you're at it: Don't repeat yourself.

Something along the lines of this should do the trick:

var element_ids = ['Onderwerp', 'Text'];
elements_ids.forEach(function (id) {
    var $element = $("#" + id);
    var color = $element.val === "" ? "#f00" : "#0f0";
    $element.css({ color: color, borderColor: color });
});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

No need to use variables use id and classes like this

$.('.TheMessage').css("color","#fff").css("border-color","#0f0"); //Use jquery like this
Mayank Singh Fartiyal
  • 867
  • 1
  • 11
  • 26
0

Please check this solution. Hope this will helpful for you.

I have tried to solve by using your own code. Thanks.

$(document).ready(function(){
$(".mailContact").click(function () {
    values = {
        Onderwerp: $('.Subject').val(),
        Text: $('.TheMessage').value,
    };
    if ($('.Subject').val() != "" && $('.TheMessage').val() != "") {
    State.sendContactMail(values);
    window.location.href = '/confirmation';
    } else {
        Onderwerp.style.color = Onderwerp.value === "" ? "#f00" : "#0f0";
        Onderwerp.style.borderColor = Onderwerp.value === "" ? "#f00" : "#0f0";
        document.getElementById("Text").style.color = document.getElementById("Text").value === "" ? "#f00" : "#0f0";
        
        document.getElementById("Text").style.borderColor = document.getElementById("Text").value === "" ? "#f00" : "#0f0";
        

    }
})
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>

<input type="text" class="form-control Subject" id="Onderwerp" placeholder="Vul je onderwerp hier in"/>

<textarea class="form-control TheMessage" id="Text"  wrap="soft" placeholder="Vul je bericht hier in"></textarea>

<a id="btn-mailContact" class="btn btn-info mailContact">Verstuur contactformulier</a>
<input type="button" class="mailContact" value="send" />

</body>
</html>
Gajjar Chintan
  • 420
  • 2
  • 8