Assigning a value to a variable using conditional IF statements and the Alert function in JavaScript

How can I set a variable value based on conditions and display an Alert in JavaScript?

Hello, I have a code that utilizes Alerts to display the results of evaluating the visibility status of two controls using jQuery.

If pnlResultados is visible, it will show: No Results or No hay Resultados. If PnlNoResultados is visible, it will show: Results Available or Si Hay Resultados.

This is the code:

Dim Banner As String = "setDDLday();
var HayDescargaSiONo = 's';
function setDDLday() {
  setTimeout(VerificaResultados,100);
  function VerificaResultados() {
    if ($('#ctl00_MainContent_PnlNoResultados').css('display') != 'none') {
      HayDescargaSiONo ='No Hay Resultados';
    } else if ($('#ctl00_MainContent_PnlResultados').is(':visible')) {
      HayDescargaSiONo ='Si Hay Resultados';
    } else {
      setTimeout(VerificaResultados,100);
    };
  };
  alert (HayDescargaSiONo);
}"

When I put an Alert immediately after assigning a value, the result is displayed correctly: ex. "No Hay Resultados". However, when I try to get the value of the HayDescargaSiONo variable with an Alert, it shows "Undefined" and fires faster than expected.

What could be going wrong?

I need to properly assign a value to the HayDescargaSiONo variable.

If it satisfies the first condition, then should be assigned "No" Else If it satisfies the second condition, it should be assigned "Yes".

Answer №1

To ensure global access, consider defining HayDescargaSiONo as $HayDescargaSiONo.

setDDLday();
$HayDescargaSiONo = 's';
function setDDLday() {
    setTimeout(VerificaResultados,100);
    function VerificaResultados() {
        if ($('#ctl00_MainContent_PnlNoResultados').css('display') != 'none') {
            $HayDescargaSiONo ='No Results Found';
        } else if ($('#ctl00_MainContent_PnlResultados').is(':visible')) {
            $HayDescargaSiONo ='Results Found';
        } else {
            setTimeout(VerificaResultados,100);
        };
    };

    alert ($HayDescargaSiONo);
}

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

Utilizing components in next.js

I am attempting to transform the following class <div className={`banner-row marquee ${playMarquee && "animate"}`}> into a format compatible with next.js. I am struggling to find a solution. ...

Toggle jQuery to hide a div and update its CSS styling

There is an anchor with the class of "hide-btn1" that I want to trigger a series of actions when clicked: The rcol-content should hide and the text should change from Hide to Show The #container width needs to increase to 2038px The table.status-table wi ...

Unusual behavior observed in AngularJs local variables

This code snippet is from the controller: cat1=[]; $.getJSON('categories/1/', function(data) { cat1 = data; //this returns a JSON object }); //cat2..4 are also JSONs $scope.pictures=[cat1,cat2,cat3,cat4,cat5]; The issue here seems to be th ...

JavaScript struggles to obtain the timezone information when Daylight Saving Time is

Can someone help me with setting a user's timezone offset for PHP through ajax? When a page is loaded with session data, if there is no pre-existing data, the following script is inserted into the page: <script type="text/javascript"> $(doc ...

Ways to transmit information from a modal to a function

Trying to figure out how to update data in an ng-grid after submitting a modal form with multiple text input controls. Should the ajax create function be called within the $scope.open controller section or resolve? $scope.open = function (size) { var mo ...

What could be causing the remove attribute API to not function properly only for the "is" attribute?

var divElement = document.createElement("div"); var innerHTMLText = "<div id='issue' is='if'> some content </div>"; divElement.innerHTML = innerHTMLText; document.body.appendChild(divElement); var newDivElement = document.qu ...

What is the method to retrieve a randomly generated class from an id?

Is there a way to target elements that have the class "my_class" within the element with the id of "info_id"? It's important to note that these elements may also have another class, which I'm not interested in selecting. <div id="info_id"> ...

The JQuery script is not producing any results

After integrating a script into my website template, it is not functioning as expected. I suspect there may be a conflict with JavaScript, but upon inspecting with firebug, I am unable to identify any abnormalities. Here is the link for reference: Link ...

Obtain the currently selected HTML element using tinyMCE

Within my editor, I have the ability to choose text and show it using the code below: alert(tinyMCE.activeEditor.selection.getContent({format : "html"})); The problem is that this function only returns text and not HtmlElement. This means I am unable to ...

Stop the print dialog box from appearing when using the Ctrl + P shortcut

I'm working on an Angular app and I want to prevent the print dialog from opening when pressing "Ctrl + P". To address this issue, I have implemented the following code: window.onbeforeprint = (event) => { event.stopPropagation(); cons ...

Is there a way to dynamically update the text in an HTML element with a randomly generated value using JavaScript?

Currently, I am working on a coding project where I am attempting to create a flip box that reveals the name of a superhero from an array when clicked by a user. The code pen link provided showcases my progress so far: https://codepen.io/zakero/pen/YmGmwK. ...

Mongoose Alert Utility Directive

const mongoose = require('mongoose'); module.exports = { init: () => { const dbOptions = { useNewUrlParser: true, useUnifiedTopology: true, autoIndex: false, reconnectTries: Number.MA ...

Requirements for two buttons

One of the requirements I have is that if the user chooses Radio button A, then a specific button should be displayed. <input type="button" disabled="disabled" name="next" value="Proceed to Payment" onclick="window.location='shipping.php#openModal ...

Hiding a div upon submission using jquery validator

I need assistance with a validator that currently hides the submit buttons and displays a "please wait" message after the user clicks submit. The issue is that it also triggers this behavior when a date field validates. Is there a way to modify the code s ...

What is the process for retrieving a value from JSON utilizing JavaScript?

Unfortunately, I am completely stumped when it comes to Javascript. I'm attempting a few solutions based on online resources. If anyone could offer some assistance, that would be greatly appreciated. Below is the JSON data provided. It has been conde ...

React Type Mutation response feedback is a valuable tool for receiving input

I am facing an issue with passing the mutation success response in my code. I have a file named change-email.tsx which calls a component file updateEmail.tsx containing a mutation function. The submit function is working fine, but I cannot figure out how t ...

Enhance the functionality of Map.set by automatically storing the new entry on the disk as well

This code is intended for development purposes only, to resume work from where I left off when restarting the local server. I aim to avoid modifying the production code in any way. My goal is to save new entries to disk when using map.set(...). Below is a ...

Steer clear of directly accessing views in AngularJS using angular-ui-router

In my AngularJS App setup, I have the following configuration: angular .module('MyApp') .config(['$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvi ...

Using a custom attribute in jQuery allows conditional statements to be executed using the

I have been attempting to create an if/else statement in jQuery using a custom attribute called "data-id". I decided to name it this way because I thought I could utilize the .data() method in jQuery, but unfortunately, it did not work as expected. Below ...

Encountered an unexpected forward slash while trying to parse JSON using

When passing a file, why does the `parseJSON` function fail? The file is stored in variable a and then I attempt to parse it using `parseJSON`. var a = "/android/contents/img/uploads/img_2A0.png"; var result = jQuery.parseJSON(a); The error being displa ...