getElementById not working

1

I have a JS function that calls an ajax and I pass the parameters (codigo, id_trigger, info1,..., info10) and passing info1 = document.getElementById('textTicker').value it turns a string, if I give console.log(info1) it returns me the text itself: "document.getElementById ('textTicker') .value "and not really the value of the field.

Is there any way I can get this value from the field?

info1 = "document.getElementById('textTicker').value"; 

function CallAjaxGenerico(codigo, id_trigger, info1, info2, info3, info4, info5, info6, info7, info8, info9, info10){

         $('#'+id_trigger).change(function(){

         $.ajax({

                ...

                data : { 

                      'CODIGO' : codigo, 

                      'INFO1' : document.getElementById(''+id_trigger+'').value, 

                      'INFO2' : info1 
                 } 
          }); 
}

and console.log (info1) returns "document.getElementById ('textTicker'). value"

    
asked by anonymous 23.04.2018 / 18:08

4 answers

1

If you really need your document.getElementById to be a string and you want to run it in your code you should * use eval () in your javascript.

var teste = 'olha';
eval("console.log(teste)")

Reference

Subject explaining a few about the possible dangers of using eval ()

    
23.04.2018 / 18:45
1

In your situation it does not make sense to use eval , in fact eval should be avoided.

If you want to pass the "reference" element can easily replace:

info1 = "document.getElementById('textTicker').value";

by:

info1 = "#textTicker";

Since you are using jQuery, it will be:

info1 = "#textTicker";

function CallAjaxGenerico(codigo, id_trigger, info1, info2, info3, info4, info5, info6, info7, info8, info9, info10){

         $('#'+id_trigger).change(function(){

         $.ajax({

                ...

                data : { 

                      'CODIGO' : codigo, 

                      'INFO1' : $("#"+id_trigger).val(), 

                      'INFO2' : $(info1).val()
                 } 
          }); 
}

You can also use the querySelector native javascript to fetch an element.

Example:

var div = "#minhadiv";

var valor = document.querySelector(div).value;
    
23.04.2018 / 19:11
0

I think your problem has to do with the DOM page, taking a look at the OS, I found the following answer , which in my view, is the same case .. however, as I reported in the comment above, there is a Jquery selector , which basically works as follows:

$("#meuid")

and to access the value:

$("#meuid").val()
    
23.04.2018 / 18:49
0

Try using the command:

info1 = document.getElementById('textTicker').value; 

No quotation marks.

    
23.04.2018 / 18:50