Send value of a variable from one method to another javascript

2

Hello, I have the following methods in javascript:

function buscarC(consulta) {
var result = "";
var buscado = "";
$.ajax({
    dataType: 'json',
    url: 'http://localhost:8080/tesisv2/webresources/generic/buscarxC?courseDisplayName=' + consulta['idCurso'] + '&page=1&row=' + consulta['row'],

    success: function (data) {
        var t = "";
        t = data.total;
        initpaginacion(t);            
        var nump;
        var n = consulta['row'];
        nump = Math.round(t / n);
        console.log(f);


        for (i = 0; i < data.elementos.length; i++) {

            buscado = '<tr class="recurso"><td>' + data.elementos[i][0] + '</a></td><td>' + data.elementos[i][1] + '</td><td>' + data.elementos[i][2] + '</td><td>' + data.elementos[i][3] + '</td><td>' + data.elementos[i][4] + '</td></tr>' + buscado;


        }

        result = '<style>table, th, td {  border: 1px solid black;}</style><table cellpadding="0" cellspacing="0" border="0" class="display" id="example"><thead><tr><th>Curso</th><th>IdEvento</th><th>Evento</th><th>Organizacion</th><th>PaisIP</th></tr></thead><tbody>' +
                buscado +
                '</tbody></table>';
        $("#contenido").html(result);
    },
    error: function () {

    }
});
}


function initpaginacion(resultadototal) {
if (total % numxpagina == 1) {
    totalpaginado = Math.trunc(resultadototal / numxpagina) + 1;
} else {
    totalpaginado = Math.trunc(resultadototal / numxpagina);
}
if (totalpaginado <= 0) {
    var paginado = "";
    document.getElementById('paginado').innerHTML = paginado;
} else {
    if (totalpaginado == last) {
        var paginado = "";
        paginado += '<ul class="pagination">';
        for (var i = init; i <= last; i++) {
            paginado += '    <li><a style="cursor:pointer;" id="pag' + i + '" onclick="buscar(' + false + ',' + i + ',' + numxpagina + ",'" + 'results' + "'" + ',null,this);"">' + i + '</a></li>';
        }
        paginado += '</ul>';
        document.getElementById('paginado').innerHTML = paginado;
    } else {
        var paginado = "";
        paginado += '<ul class="pagination">';
        for (var i = init; i <= last; i++) {
            paginado += '    <li><a style="cursor:pointer;" id="pag' + i + '" onclick="buscar(' + false + ',' + i + ',' + numxpagina + ",'" + 'results' + "'" + ',null,this);"">' + i + '</a></li>';
        }
        paginado += '    <li><a style="cursor:pointer;" onclick="next()">&raquo;</a></li>';
        paginado += '</ul>';
        document.getElementById('paginado').innerHTML = paginado;
    }
}
}

function buscar(a, b, c, d, e, f) {
alert(f.text);

}

I want to obtain the value of the variable f as seen in the search metere (with an alert that indicates its value), to use it in the method buscarC, how can I obtain that value to be able to use it in the method buscarC.

    
asked by Gerardo Gutiérrez 08.08.2017 в 19:04
source

2 answers

1

Hello, you can use global variables so that the searchC function has the answer of f, I'll give you an example:

var mensaje = “Mensaje de prueba”;


function muestraMensaje() {
  alert(mensaje);
}

For your case you must declare them as global

var a, b, c, d, e, f;
function buscarC(consulta) {...}
function buscar(a, b, c, d, e, f) {...}
    
answered by 08.08.2017 в 19:11
1

You could generate a global variable (that is not inside any function) and assign the value of f to said variable in the function buscar .

var dato = "";

function buscar(a, b, c, d, e, f) {
   dato = f.text;
}

And then you can use that variable dato in the function buscarC .

function buscarC(consulta){
     //Tu código
     console.log(dato);
}
    
answered by 08.08.2017 в 19:12