Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
Cannot assign to function call

I'm new in learning JavaScript. Just for some test I built this function and call it and assign a variable in it to a complete result. Is there a way use the function like this example?

Here I want to just pass #country in the dom(i) function. So it should be getElementById("maid"), and after calling it, assign = maid to put this maid variable after innerHTML =.

I'm wondering is it possible to write JavaScript function like this way?

var maid = "Made In Yiappa";
function dom(i) {
    var docs = document.getElementById(i).innerHTML;
    return docs;
}
dom("country") = maid;
<p> The result is: <span id="country"></span> </p>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You must pass it as an argument to that function:

var maid = "Made In Yiappa";
function dom(elementId, txt) {
    document.getElementById(elementId).innerHTML = txt;
}
dom("country", maid);
<p> The result is: <span id="country"></span> </p>

about 4 years ago · Juan Pablo Isaza Relatório

0

You can’t assign a value to the result of a function call. You need to pass it as a function argument.

var maid = "Made In Yiappa";
function dom(i, text) {
    document.getElementById(i).innerHTML = text;
}
dom("country", maid);
<p> The result is: <span id="country"></span> </p>

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. The key issue is that you're returning the value of the innerHTML property from the function but you can't then assign a string to it because innerHTML can only be set when it's attached to an element.

  2. querySelector maybe a better method choice. You can then pass in your #country selector as you mentioned in your question.

  3. So, we can remove some of the functionality from the function, and just have it return the element using the selector. Then we can assign main to the innerHTML of that returned element (although innerText might be more appropriate).

var maid = 'Made In Yiappa';

function dom(selector) {
  return document.querySelector(selector);
}

dom('#country').innerText = maid;
<p> The result is: <span id="country"></span> </p>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda