How to pass the content of a variable from one if (add) to another variable of another if (load)?

0

Pass the contents of a variable from one if () to another if (). in jquery My question is how to do that, here I leave a bit of code to make my question clearer.

(function ($) {

this.each(function () {
var easyTree = $(this);
 //desde aqui comienza el problema -************

  if (options.addable) {
      $(createInput).find('.confirm').click(function () {                       
                    if ($(createInput).find('input').val() === '')
                     return;
     // AQUI es donde uso el texto input de entrada y lo pongo en una variable
     var input= $(createInput).find('input').val();
});

    if(options.load){
// ahora yo quiero usar el contenido de esa variable en esta nueva condicion 
//pero no se como llamar a la variable 'input' y a su contenido

         }
  })(jQuery);
    
asked by Lun 14.11.2017 в 15:57
source

2 answers

1

What you must do is declare your input variable as a global and not a local variable, that is:

(function ($) {

var input;

this.each(function () {
var easyTree = $(this);
 //desde aqui comienza el problema -************

  if (options.addable) {
      $(createInput).find('.confirm').click(function () {                       
                    if ($(createInput).find('input').val() === '')
                     return;
     // AQUI es donde uso el texto input de entrada y lo pongo en una variable
     input= $(createInput).find('input').val();
});

    if(options.load){
// ahora yo quiero usar el contenido de esa variable en esta nueva condicion 
//pero no se como llamar a la variable 'input' y a su contenido

         }
  })(jQuery);
    
answered by 14.11.2017 в 16:03
1

You should know that javascript is asynchronous and is oriented to Events. I tell you this because it seems that you do not have it in mind when you write your code. I'll explain:

        (function ($) {
var input;
        // Por qué haces esto? no entiendo por qué intentas recorrer todo el doom?
        this.each(function () {
        var easyTree = $(this);
        // Aquí validas que la condición se cumpla, esto es correcto.
          if (options.addable) {
              // aquí defines el evento "clic" a un elemnto, esto quiere decir que se ejecuta unicamente cuando se haga clic sobre el elemento.
              $(createInput).find('.confirm').click(function () {                       
                            if ($(createInput).find('input').val() === '')
                             return;
             // Aquí defines una nueva variable con el valor de un elemento "input", 
             //pero recuerda que esto sucede solo cuando alguien ejecute el evento, es decir, cuando alguien haga clic en el elemento "$(createInput).find('.confirm')"
             input= $(createInput).find('input').val();
        });

            if(options.load){
                    // Aqui no puede usar la variable, porque cuando este código se ejecute, (seguramente es al cargar la pagina) nadie ha hecho click en el elemento, y aún no se ha definido.

                 }
          })(jQuery);

What you must do is access the item just when the event is launched like this:

(function ($) {
var input;
//Esto lo voy a dejar, pero insisto que no entiendo la razón.
this.each(function () {
var easyTree = $(this);

  if (options.addable) {
      $(createInput).find('.confirm').click(function () {                       
                    if ($(createInput).find('input').val() === '')
                     return;
     // AQUI es donde uso el texto input de entrada y lo pongo en una variable
     var input= $(createInput).find('input').val();
     if(options.load){
            //Aquí ya puedes acceder a la variable :)
         }
});

  })(jQuery);
    
answered by 14.11.2017 в 23:56