This is my code:
$(document).ready(function () {
if ($("#write_text_id").text() === "Saurav & Kunal Are God") {
$(".submit_form_field").addClass('item-3');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="test">
<textarea id="write_text_id" class="textarea small" tabindex="105" placeholder="Write Saurav & Kunal Are God" rows="4" cols="50"></textarea>
<input id="submit_btn_id" class="submit_form_field" type="submit" value="Submit">
</div>
I’m expecting that if I write "Saurav & Kunal Are God" in the textarea then submit button will add class item-3
.
3
Answers
You should check when input typing and not one time. For this, with jQuery, you can use
on('input', function)
as explained here.I personally remove the class when it’s wrong, to make it disappear when the text change again.
What you’re looking for is an
EventListener
instead ofdocument.ready
to watch for changes to the textbox.Notice that
document.ready
will only get called once from theconsole.log
I added when the document is originally loaded:However, upon using an
EventListener
you will get the desired result when clicking the submit button:It is also worth noting that I used
$("#write_text_id").val()
since the.text()
was not returning anything.