How to put a link inside a String?

Asked

Viewed 1,008 times

2

How do I place a link inside a string?

var results = [
    {

        text: "Quero colocar um link aqui exemplo <a href="#"> Link </a>"; 
  },
  • 2

    Just slip the quotes "<a href=\"#\">". By the way, when you post a question, use the language tag.

3 answers

2

Answering the question:

Form 1 - using single quotes:

text: "Quero colocar um link aqui exemplo <a href='#'> Link </a>";

Form 2 - using exhaust:

text: "Quero colocar um link aqui exemplo <a href=\"#\"> Link </a>";

Form 3 - inserting a variable with the URL:

meulink = "#";
...
text: "Quero colocar um link aqui exemplo <a href=" + meulink + "> Link </a>";

Form 4 - Escaping the whole text:

text: `Quero colocar um link aqui exemplo <a href="#"> Link </a>`;

Extra information

Based on your comments, you are probably confused when creating this array and object, as well as having difficulty accessing because of the first error. Behold:

var novo_array = [ 0, 1, 234 ];
var novo_objeto = { idade: 23, nome: "José da Silva", funcao: "Desenvolvedor" };
var objeto_em_array = [ 
    { 
        texto: "Objeto dentro do índice 0 do array", 
        texto2: "este também" 
    } 
];

Basically you are creating objects within an array. How your array has index 0, you access the object items like this - according to the above example:

 objeto_em_array[0].texto

I don’t know why you nested them both or if it’s necessary. If it’s not, you can only use the object.

I will provide a functional example of what you tried to do. Note only the executable result and Javascript. The rest is unnecessary for understanding and can be ignored as it only serves to see the execution (visual). See

var resultados = [ 
  { 
    texto: "Clique aqui para abrir o poderoso <a href=\"https://www.google.com\">Google</a>!",
    texto2: "Another object"
  }, 
  "Outro item do array",
  "Mais um item do array"
];

var popular = function(id){

  alvo = document.getElementById(id);

  alvo.innerHTML = resultados[0].texto;

};
body { font-size: 12px; }

#nav { width: 22em; text-align: center; }

#formulario { float: left; width: 5em; margin: 0 2em 0 0; }

#saida { float: left; width: 15em; }

#cabecalho { background-color: #ffffaa; color: 333; padding: 0.5em; }

#destino { padding: 0.5em; }

.limpar { clear: both; }
<div id="nav">
  <div id="formulario">
    <form> 
      <input type="button" value="CLIQUE" onclick="javascript: popular('destino');" /> 
    </form>
  </div>

  <div id="saida" >
    <div id="cabecalho">SAÍDA</div>
    <div id="destino"></div>
  </div>

  <div class="limpar"></div>
</div>

  • I appreciate your help, but none of these methods worked.. You think because I’m using an Array, it changes something?

  • @Ogaiteuqireh using arrays, no. But you are using array and objects. The example provided by you, specifically text, is an object within the array results in the index 0. The access would be results[0].text I made modifications; take a read.

1

Just use different quotes with which you opened the string.

  
  (function(){
  var results = [
    {

        text: 'Quero colocar um link aqui exemplo <a href="#"> Link </a>'
  }]
  	document.getElementById('teste').innerHTML = results[0].text
  })()
<p id="teste"></p>

1

There are three ways you can do this by placing a " " (bar) in the desired character, starting the string with single or double Quotes or using template literals (template strings).

The first way is to start with single quote and use double quote when needed:

var texto = 'Quero colocar um link aqui exemplo <a href="#"> Link </a>';

The second way is you put the "" (backslash), getting the following way:

var texto = "Quero colocar um link aqui exemplo <a href=\"#\"> Link </a>";

The third way is to use template literals, starting and finishing the string with sign " ` " (Grave Accent)

var texto = `Quero colocar um link aqui exemplo <a href="#"> Link </a>`;

Note that this last approach you will need to be aware as to the compatibility of the browsers. A good website query is this link

For more information on template literals, see this documentation

  • Thank you very much, but it didn’t work, I’m using an array, does it influence anything? thanks

  • It does not influence, it would be the same thing. How are you assigning the value to the array? Gives some error message?

  • Not from the error, just without the link type: text <a href=""> link </a> , I am assigning it like this: $(". js-renderResults"). on("click", Function() { var result = getResults(); }); $(". js-resultsText"). text(result.text);

  • In this case, I think it has to do with assigning the array by position. Read José’s answer that he is explaining more fully how to do this.

Browser other questions tagged

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