If there is xx, then yy will follow suit

Hello, I am relatively new to JavaScript and am currently attempting to develop a script using Greasemonkey that will automatically fill out a form on a webpage.

Essentially, what I need the script to do is check for a specific error code on the page. If the code exists, I want the script to simply click the "Back" button.

<div>
  <div id="registration_error" class="errBlock" style="color:Red;">
    <ul><li>Address is required.</li></ul>
</div>

I have attempted several different approaches in my code (even though I am not entirely sure if they are correct), but unfortunately, none of them have yielded success. For example:

if(document.getElementById("registration_error").innerText.Contains("Address is required.")
{
 document.getElementsByClassName('btnRegister')[0].click();
}

I also tried variations such as:

if(document.getElementsByClassName('errBlock').innerText.Contains("Address is required")

if ("#registration_error").innerText.Contains("Address is required")

Despite trying numerous combinations within my limited knowledge, I have yet to establish a functional if condition. While the button-clicking aspect works fine, I am struggling with this particular aspect.

Thank you in advance for any assistance or insight provided.

Answer №1

The initial code snippet you attempted is the most accurate.

Initially, the segment

document.getElementById("registration_error")
is correct (although a quotation mark is missing in yours).

Subsequently, your intention is to retrieve the HTML content within the registration_error div. The appropriate method for achieving this is by using .innerHTML. (

.innerText</code did not function properly in my case)</p>

<p>The last section where you are checking whether the HTML contains "Address is required" is incorrect. The proper way to ascertain if a specific text exists within a string is to utilize <code>"hello world".indexOf("llo")
. This operation will provide the position of the string if located, or -1 if it is absent from the string.

Hence, your final if statement should appear as follows:

if(document.getElementById("registration_error").innerHTML.indexOf("Address is required.") !== -1){
    document.getElementsByClassName('btnRegister')[0].click();
}

This should effectively resolve the issue at hand. It's important to note that Javascript is indeed case-sensitive, so accuracy is crucial.

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

When employing JQuery toggle, why does the script only condense the initial value in my iterator?

Upon testing this code on my server, I encountered an issue where only the first value in my iterated list would minimize or maximize when clicking on the options. How can I modify it so that all values in the list can be minimized and ...

Please be patient and allow the function to complete before moving forward

Currently, I am in the process of building my first nodejs/DynamoDB application and it involves working with JavaScript for the first time. My goal is to save a new record after scanning the table and using the results of the scan as a basis. After doing s ...

I can't seem to shake off this constant error. Uncaught TypeError: Unable to access property 'classList' of null

I am facing an issue with the "Contact Me" tab as it does not display its content when clicked. Here is the code snippet: <body> <ul class="tabs"> <li data-tab-target="#home" class="active tab">Home< ...

Guide to encapsulating JavaScript fetch within a function - dealing with unhandled promise rejection

I'm attempting to create a wrapper function for JavaScript's fetch command. I found the following example code on this post: function fetchAPI(url, data, method = 'POST') { const headers = { 'Authorization': `Token ${get ...

Component not displaying API data despite being visible in the console

Currently dealing with an issue where I am trying to make a simple API call to and then display the fetched data in my component. Strangely, the data is not showing up on the page, although it is being correctly displayed when I console log it. const Di ...

Excluding a Spec File in Your Protractor Configurations

I have a scenario where I have 10 spec files all named *********.test.js. I need to run tests on all 9 of these files, excluding the file named Idontwantyou.test.js. Currently, I am locating my spec files in the config.file using: specs: ['*.test.js ...

Customize the filename and Task Bar name for your Electron app when using electron-packager

My application uses electron packager to build the app on Mac. Here is my package.json configuration: { "name": "desktop_v2" "productName": "desktop v2", "version": "1.0.0", "license": "MIT", "scripts": { "build": "node --max_o ...

Why does Vue 3 refuse to refresh an <img> element, while it successfully refreshes all other components within the same component?

In my current Vue project, I have set up multiple link cards (<a class='card'></a>) inside a "deck" container (<div class='deck'></div>). The implementation for these elements is relatively straightforward: <s ...

Creating a 2D array in JavaScript filled with data retrieved from a PHP-MySQL database

Having just started learning Javascript/PHP, I have encountered a particular issue that I hope someone can assist me with. When attempting to generate a javascript array from PHP, everything works smoothly until I reach the column 'Function' in m ...

Discover the Elements that have been incorporated through the use of JavaScript

I am facing an issue with my ASP site where users can add Label elements. The problem is I am not able to track the labels that were added or their IDs. All I know is that they will be inside the Panel called pnl_Added. Once the user has finished adding la ...

Issue: parsing error, only 0 bytes out of 4344 have been successfully parsed on Node.js platform

I've been attempting to utilize an upload program to transfer my files. The specific code I'm using is as follows: app.post('/photos',loadUser, function(req, res) { var post = new Post(); req.form.complete(function(err, fields, fil ...

Dealing with undefined variables in Angular using ng-show

There are two directives that are displayed based on the configuration of a variable. This was working visually, but I encountered an issue during acceptance tests. I tried to test whether both directives would be hidden if the <settingElement> had n ...

JQuery - Issue: Invalid syntax detected in the expression: #

I'm facing an issue with some tabs inside an accordion that don't seem to be functioning properly. The console is showing the following error: Error: Syntax error, unrecognized expression: # Despite searching for a solution online, I can&apos ...

How can I ensure that all elements in a list are affected when a change occurs in Vue.js?

I'm in the process of developing a website that heavily relies on Vue for managing the majority of the user interface. The centerpiece of the site is a dynamic video list component that gets updated whenever a specific URL pattern is detected. The pr ...

Adjusting the color of the legend on a LineChart in ExtJS 4 on-the-fly

I've been trying to find information on how to modify the color of the x legend in a Line chart without success. Can anyone help me with this? I have included an image of the chart for reference. ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Issues with AngularJS's ng-options functionality are hindering its performance

Having trouble with ng-options not working as expected? Despite following the required structure, it seems like you're getting the same result. Don't worry, I've been researching the topic endlessly and could use some help from you guys. HT ...

Loading V-Select with Data from JSON Using Vue.js

I tried to populate my v-select multiselect element using a JSON object, but unfortunately it did not work as expected. Here is the result I encountered: https://i.sstatic.net/kd1Gl.png <v-select v-model="serviceValues" :items="serviceO ...

Submitting form based on HTML select input issue

I am facing an issue with a select form where the value resets to "10" every time I send the form. Is there a way to keep the selected option, for example "20", after submitting the form? Below is the code snippet: <form method='get' name=&a ...

Customizing Ext JS/Sencha Chart framework based on certain conditions

As someone who is new to Ext JS and Sencha charts, I have encountered a challenge with one of the charts in our application. Specifically, I needed to hide the dashes on the X-Axis of that particular chart. Our application is built using Ext JS version 5.1 ...