Using JQuery to verify if a certain letter or word has been entered by the user in a textarea

Can anyone assist me in checking if a user has entered a specific letter or word and, if correct, display a button?

I would appreciate any guidance!

Here is my current code snippet:

 $(document).ready(function() {
   $(".textArea").keyup(function() {
     if ($(this).val() == 'a') {
       $(".continue").css("visibility", "visible");
     }
   });
 });
body {
  margin: 0;
  padding: 0;
  background-color: #3f2860;
}
.codeArea {
  width: 50%%;
  height: 500px;
  border: 2px solid #ef6d3b;
  box-sizing: border-box;
  font-size: 20px;
  background-color: transparent;
  color: #ffffff;
  outline-width: 0;
  position: relative;
  float: left;
  margin-right: 5px;
}
.textArea {
  width: 100%;
  height: 100%;
  resize: none;
  font-size: 20px;
  background-color: transparent;
  color: #ffffff;
  outline: none;
  border: none;
  white-space: normal;
  position: relative;
  float: left;
}
.boxContainer {
  width: 98%;
}
.boxContainer {
  margin: 0 auto;
}
.continue {
  background-color: #ef6d3b;
  width: 6em;
  text-align: center;
  font-size: 15px;
  border: none;
  height: 25px;
  color: #000000;
  outline: none;
  cursor: pointer;
  border-radius: 5px;
  text-transform: uppercase;
  position: relative;
  visibility: hidden;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<div class="codeArea">
  <textarea class="textArea">
    &#60;h1&#62;Test&#60;&#47;h1&62; &#60;style&#62; h1&#123; &#125; &#60;&#47;style&#62;



  </textarea>

</div>

<div class="buttonContainer">
  <a href="#">
    <button class="continue" type="button">Continue</button>
  </a>


</div>

Answer №1

The issue may lie in how you are verifying the content of your textarea. Currently, you are extracting all the text and comparing it with =='a'. A better approach would be to use the following method:

if($(this).val().indexOf('keyword') !== -1)

In this case, 'keyword' represents the specific term you are looking for. This way, you can search for the term within the textarea and determine its presence.

Answer №2

Within this section of code:

$(document).ready(function() {
   $(".textInput").keyup(function() {
     if ($(this).val() == 'a') {
       $(".next").css("display", "block");
     }
   });
 });

The $(this).val() initially contains the content

<h1>Example</h1> <style> h1{ } </style>
, making it impossible to match it with 'a' unless you delete all the text and enter just the letter 'a'.

It is necessary to utilize indexOf('a') in this way...

$(document).ready(function() {
   $(".textInput").keyup(function() {
     if ($(this).val().indexOf('a') !== -1) {
       $(".next").css("display", "block");
     }
   });
 });

Answer №3

If you currently have a condition where the button is only shown if the exact contents of .textarea are equal to 'a', including any leading or trailing whitespace, then you may need to consider using .indexOf() instead.

To achieve the desired functionality of "show me the button when .textarea contains 'a'", you can modify your code as follows:

 $(document).ready(function() {
       $(".textArea").keyup(function() {
         if ($(this).val().indexOf('a') !== -1) {
           $(".continue").css("visibility", "visible");
         }
       });
     });

You can test the above example with your original code by visiting: https://jsfiddle.net/m410xphk/8/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the most effective way to obtain a customer's latitude and location by prompting them to drop a pin on Google Maps?

My Android app has an API, but on my website I'm wondering how I can retrieve a person's location by having them place a marker on a Google Map. Is there a standard method for this? I need to obtain the latitude and longitude coordinates and send ...

Sending a POST request via HTTPS results in a 403 error (Forbidden) being triggered

Utilizing the jQuery fileDownload tool (developed by John Culviner), I am loading dynamically generated DOCX files from a JBoss server using RESTEasy. Both the application and the files are within the same domain. This process works seamlessly when retrie ...

send parameter when clicking a link rather than using a hashtag

One interesting feature on my website is a menu link with the attribute title=.roc. When this link is clicked, I want to extract this attribute and click on an element with the same attribute on the destination page. Let's consider a scenario where I ...

Blazor Razor Component Library (RCL) Lacks CSS IntelliSense

I am encountering an issue with the CSS intellisense in my razor class library (RCL) that houses all the pages of my Blazor application. It appears that the CSS intellisense is not functioning within the RCL unless I modify the RCL .csproj xml tag From Sdk ...

Refresh table in php without the need to reload the entire page

Currently, I am retrieving data in a table from the database and have three buttons - update, delete, and an active/inactive button. Each button has its own functionality, but I need to reload the updated data in the table after any event without refreshin ...

Customize Joomla Module Styles

Here in the JhotelResrvation Module area, I've identified where the module files are located. However, I'm unable to make changes to the CSS despite trying custom CSS as well. My initial plan was to add padding to the dark-transparent box using c ...

When you click on one item in a jQuery selector array, the action is being applied to all elements within the array

I've been working on implementing a cheat function for a minesweeper game that reveals a random non-bomb square (line 279) and its safe neighboring squares (the 8 squares around it without bombs). However, I'm encountering an issue where using $ ...

Utilizing AngularJS to calculate elapsed time and continuously update model and view accordingly

Situation Currently, I am interested in developing a web application that tracks a specific data set based on the time since the page was loaded. For example, "how many calories have you burned since opening this webpage?" I am still trying to grasp the ...

What is the process for assigning variables to modules using RequireJS?

Is there a way to define variables for modules in RequireJS? In simpler terms, how can I achieve the equivalent of the following using RequireJS: var fs = require('fs'); var child_process = require('child_process'); I am looking to s ...

Is there an array containing unique DateTime strings?

I am dealing with an Array<object> containing n objects of a specific type. { name: 'random', startDate: '2017-11-10 09:00', endDate: '2017-11-23 11:00' } My goal is to filter this array before rendering the resu ...

Generating a multiplication grid using PHP

I'm currently facing a challenge regarding the presence of an additional "0" box on my multiplication table. Below is the code that I have been working with: $cols = 10; $rows = 10; $number = 0; $number2 = 0; echo "<table border=\"1\"> ...

Responsive DataTable in Bootstrap 5 automatically hides columns to ensure optimal display on different

Is it possible to create a responsive table using DataTable 1.13.4 and Bootstrap 5.2.3? I encountered an issue while testing the table on a mobile device (using Firefox, not an actual device) where a small horizontal scrollbar appears, causing some columns ...

Avoid duplicate borders on columns that adjust their size based on the screen width

Picture yourself utilizing Bootstrap 4 and displaying various cells, each with unpredictable numbers. .cell { border: 1px solid black; } <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"> < ...

How can you use CSS to style an image's title attribute?

I am trying to customize the text displayed when hovering over an image. Despite my efforts, the code I used doesn't seem to be working properly: <body> <img src="img.jpg" title="<span class='title'>title</span>" &g ...

Instructions on how to successfully send an AJAX request without having to open the webpage while utilizing Google App Engine

When sending AJAX requests without having to open a webpage on Google App Engine, you can utilize JavaScript libraries like jQuery $.ajax or $.post. However, if you want to send an AJAX request and receive the return value without opening a webpage, espec ...

Utilizing JQuery UI with JSON data within the Codeigniter framework

Here is a code snippet for a controller: public function fetchAutocomplete() { $this->load->model('Auto_model'); if (isset($_GET['term'])) { $result = $this->Auto_model->get_data_auto('ta ...

The response from a jQuery ajax call to an MVC action method returned empty

I am working on an inventory application with the following layout: <body> <div class="container" style="width: 100%"> <div id="header"> blahblahblah </div> <div class="row"> <div id="rendermenu ...

Using CSS3 to Create a Pie Chart

Looking to design a symmetrical pie chart featuring 6 pieces, each at 60 degrees, using only html and CSS3. Came across an article , but the concept is unclear to me. Seeking assistance or explanation on the classes mentioned in the link: .pieSliceBlue, ...

Is it possible for me to create a nested component without having to make a new file

What I desire: Home.vue <template> <div> <p>{{message}}</p> <ChildComponent/> </div> </template> <script setup> import { ref, defineComponent } from 'vue'; let message = r ...

Splitting a row into four columns that will appear consistent on mobile devices

Is it possible to create a row with 4 columns in Angular 6, but have it display as 2 rows with 2 columns each on mobile devices? <div class="row" id="info" *ngIf="this.details"> <div class="col-md-12 mb-3" id="heading"> <h3>Meeting ...