getElementById not working

Asked

Viewed 302 times

1

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

Is there any way I can get that 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 the console.log(info1) returns "Document.getElementById('textTicker'). value"

  • 1

    You’d have to show the code because it shouldn’t happen in normal situations.

  • do not quote, or you will see a string.

  • I’m using a php function that calls this function from the js file. In this js Document.Elementbyid file comes as string

  • Okay, but that way the value of info1 will always be the string document.getElementById('textTicker').value and not the value of #textTicker.... Javascript does not execute commands and methods inside quotes... everything inside quotes is string for JS

  • tried using $("#textTicker"). val() or $("#textTicker"). text() ??

  • Do not put code referring to the question in comments. Instead, edit the question and put the code there, formatting it with Ctrl + k or with the button {} of the editor

  • @Danielat. you’ll have to wear one eval() In this string, it is something very funny but it is what you do if you need to execute JS code from a text: https://jsfiddle.net/tbyqy4Lz/ - Docs.

  • @Renan using Eval() worked! Thank you so much!

  • Do not use Val, I doubt very much that justifies your case. Why not use the Jquery selectors and take the value with val(). By the way you use this method $('#'+id_trigger).change(function(){ and then passes a string? Review its implementation. Not critical is advice.

Show 4 more comments

4 answers

1

1

In your situation it makes no sense to use the eval, moreover the Eval should be avoided.

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

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

for:

info1 = "#textTicker";

Since you are using jQuery you 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 querySelector javascript native for fetching an element.

Example:

var div = "#minhadiv";

var valor = document.querySelector(div).value;
  • I tried to do a very generic function and that in some cases, info1 may be empty, it would give reference problem to $(" "). val()? that was my biggest problem of simply passing the field id

  • If you pass $(""). val() you will return undefined, however may use this designation to manipulate the final result. if (typeof info2 != 'undefined')

0

I believe your problem has to do with GIFT from the page, taking a look at the OS, I found the following reply, 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()

0

Try using the command:

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

Quote-free.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.