-1

I'm developing a website, which is using jQuery.Inside this code I need to change the value of a variable inside a Jquery selector and get the changed value after it.Is it possible to do that?How can I achieve this?If possible, could show me a snippet/example code?

I've tried declaring the variable global, but without success too.

var index;
$(document).ready( function(){

    $("#myButton1").click(function(){ //selector number 1
        index = 1;
    });

    $("#myButton2").click(function(){//selector number 2
        index = 2;
    });

    //after, i need the value of the index for another selector
    //look this next selector is fired at the same time as the previous one!

    $("button[id^=myButton"+index+"]").click( function(){  //selector number 3
        ...
    }
}

How can I make the selector number 1 or 2 fire after the selector number 3?Is it possible?

Lucas Studart
  • 73
  • 1
  • 7
  • Can you create a new variable = index, then modify and alert that variable? – Claudio Feb 03 '19 at 19:05
  • Related post: [Why is my variable unaltered after I modify it inside of a function?](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Jonathan Lonowski Feb 03 '19 at 19:07
  • I've reformulated my question, please take a look. – Lucas Studart Feb 03 '19 at 19:15
  • This appears to be an [XY Problem](http://xyproblem.info/). Provide a higher level explanation of what you need to accomplish along with enough html to create a [mcve] – charlietfl Feb 03 '19 at 20:36

3 Answers3

2

Javascript executes code asynchronously. In other words, whole code executes at the "same time." So first, it will execute var index;. Since the jQuery .click is waiting for you to click the button, it will skip both of the .click functions and move on to the alert. Since index is undefined, it will say index=undefined. To fix that, move the alert's inside the .click function so that the alert will execute after you click the button.

var index;

$("#button1").click(function() {
  index = 1;
  alert("index = " + index);
});

$("#button2").click(function() {
  index = 2;
  alert("index = " + index);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="button1"></button>
<button id="button2"></button>

Or you could do it this way:

var index = 0;

$("#button1").click(function() {
  index = 1;
});

$("#button2").click(function() {
  index = 2;
});

setTimeout(function() {
  alert("index = " + index);
}, 5000);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="button1"></button>
<button id="button2"></button>

The above method basically executes the alert after 5 seconds, so you can change the value of index as many times as you want in those 5 seconds. The default value is 0, but if you click the first button within those 5 seconds, the value of index changes to 1. Same for the second button.

Aniket G
  • 3,471
  • 1
  • 13
  • 39
  • I've reformulated my question, please take a look. – Lucas Studart Feb 03 '19 at 19:15
  • The jQuery `.click` function you had does change the variable index. It's just that the alert executes before you give index any value. Idk how to do this, but lets say you somehow added a wait time for 5 seconds, and clicked the button to change index to 1 before those 5 seconds, the alert would say `index = 1` – Aniket G Feb 03 '19 at 19:20
  • I've updated my solution. Look at the second snippet. I think that should explain this to you better. – Aniket G Feb 03 '19 at 19:24
1

The things that happen when you click one of the buttons are those you define inside the click-handler function (see here):

$("#button1").click(function(){
    window.index = 1; // only this line!!!
});

Your call to alert() resides inside the ready-funtion and is therefore only called when the page is loaded. You need to put the alert inside the click handlers to call it "on click". Doing so, all three versions should work. Should look like this:

$("#button1").click(function(){
    index = 1;
    alert(index);
});

After your edit:

Same thing here: the selector string after you comment is created at the time of the page load, before any button is clicked. and never again after that. At that moment, it evaluates to "button[id^=myButtonundefined]" because index has no defined value yet. T## is function therfore will be executed whenever you click a button whose ID starts with myButtonundefined - probably never.

Everything you want to achieve, for which you need the value of index you need to execute inside the click-handler function. e.g.:

$(document).ready( function(){

    $("#button1").click(function(){
        $("button[id^=myButton1]").click( function(){ 
            ...
        });
    });

    $("#button2").click(function(){
        $("button[id^=myButton2]").click( function(){ 
            ...
        });
    });
}

or you could try the following approach, which installs a click-handler on all myButton...'s and therein checks if the corresponding button... has been clicked before:

var index;
$(document).ready( function(){

    $("#button1").click(function(){
        index = 1;
    });

    $("#button2").click(function(){
        index = 2;
    });

    //after, i need the value of the index for another selector:

    $("button[id^=myButton]").click( function(){ 
        if (this.id == 'myButton'+index) {
            ...
        }
    }
}
Beppo
  • 176
  • 7
0

How to change a global variable inside jQuery selectors?

Don't use a global variable in this instance. You have a chance of a variable collision with any other code (jQuery or any other script you use). You can simply place index inside your document ready and use it in your example code and it will work without any chance of collision.

    $(document).ready( function(){
      var index;

      $("#button1").click(function(){
        index = 1;
      });

      $("#button2").click(function(){
        index = 2;
      });

      //after, i need the value of the index for another selector:

      $("button[id^=myButton"+index+"]").click( function(){ 
      });
      
      $('.js-getcurrentvalue').on('click', function() {
        $('#currentvalue').val(index);
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" class="js-getcurrentvalue" value="Get Current Value of Index"/><input type="text" id="currentvalue" /><br/>
<input type="button" id="button1" value="button1" /><br/>
<input type="button" id="button2" value="button1" /><br/>

But at the same time, the selector $("button[id^=myButton"+index+"]").click( function(){ }); fires.So, both are executed at the same time.I need that the second selector execute always after the first selector.Do u know how can I accomplish this?

This is not the original question you asked. Please read what an XY Problem is so your future questions can be answer correctly.

Highly recommended reading: Decouple your HTML, CSS and Javascript.

First we need to understand that each of these statements that attach an event handler onto an element all run before the event handler can be executed. So in my previous example the following events are registered:

$("#button1").click()
$("#button2").click()
$("button[id^=myButton]").click();
$('.js-getcurrentvalue').on('click')

You'll notice that I've done what any compiler would do and reduce the variable into it's actual value. At the time the event handler is attached, index has no value. Since this isn't what you want, you could write it like:

$("button").click(function() {
  var $this = $(this);
  var id = $this.prop(id);
  if ($this.is("[id^=myButton"+index+"]") {
    // do something as index changes
  }
});

But it's really ugly and introduces an abstraction of a value to used to compare. It's also very tightly coupled, that is we have to place an event on any object we want to change index and we have to write more code for each button. Yikes. Instead we can use classes and the data-attribute with data() to simplify and make this more robust.

$(document).ready( function(){
      var selector;

      $(".js-enable-button").on('click', function(){
        selector = $(this).data('selector');
      });

      $('.js-enable-me').on('click', function() {
        var $this = $(this);
        if ($this.is(selector)) {
          alert($this.val());
        }
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" class="js-enable-button" value="Enable button -->" data-selector="#button1" />
<input type="button" class="js-enable-me" id="button1" value="Am I working?" /><br/>

<input type="button" class="js-enable-button" value="Enable button -->" data-selector="#button2" />
<input type="button" class="js-enable-me" id="button2" value="Or am I working?" /><br/>

Now the code is not limited to an Id. It's also not limited to a single selector. You could go crazy and just by adding only html the following continues to work for all elements. Notice I've added no additional code.

$(document).ready( function(){
      var selector;

      $(".js-enable-button").on('click', function(){
        selector = $(this).data('selector');
      });

      $('.js-enable-me').on('click', function() {
        var $this = $(this);
        if ($this.is(selector)) {
          alert($this.val());
        }
      });
    });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="button" class="js-enable-button" value="Enable button -->" data-selector="#button1" />
<input type="button" class="js-enable-me" id="button1" value="Am I working?" /><br/>

<input type="button" class="js-enable-button" value="Enable button -->" data-selector="#button2" />
<input type="button" class="js-enable-me" id="button2" value="Or am I working?" /><br/>
<br/>
<input type="button" class="js-enable-button" value="I enable the three below me using id" data-selector="#id1,#id2,#id3" /></br>
<input type="button" class="js-enable-me" id="id1" value="id1" /><br/>
<input type="button" class="js-enable-me" id="id2" value="id2" /><br/>
<input type="button" class="js-enable-me" id="id3" value="id3" /><br/>
<br/>
<input type="button" class="js-enable-button" value="I enable the three below me using a class" data-selector=".enable" /></br>
<input type="button" class="js-enable-me enable" value="I'm .enable 1" /><br/>
<input type="button" class="js-enable-me enable" value="I'm .enable 2" /><br/>
<input type="button" class="js-enable-me enable" value="I'm .enable 3" /><br/>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
  • Works fine for me. Updated your example a little. – Erik Philips Feb 03 '19 at 19:25
  • I understood what is happening.When you click a button, says button2, it attributes 2 to de "index" variable. But at the same time, the selector $("button[id^=myButton"+index+"]").click( function(){ }); fires.So, both are executed at the same time.I need that the second selector execute always after the first selector.Do u know how can I accomplish this? – Lucas Studart Feb 03 '19 at 19:33
  • I've updated my question.I've read your answer and I saw that we were diverging about the name of the buttons.Take a look. "button1" was actually "myButton1" and "button 2" was actually "myButton2".I've edited the original post so now we can clearly see that the "selector number 3" is the same as "selector number 1" or "selector number 2", but writted in another manner.Now, my question is, is it possible the same selector, called twice, in one of the calls be fired a little bit before the other.Is it possible? – Lucas Studart Feb 03 '19 at 20:35