Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting String to HTML - string to " a href" element

Hello I am having some trouble getting some HTML links to add to my HTML page. I have tried searching around but nothing has helped thus far.

My page will initially load a snippet:

<div style="display: inline-block; color: rgb(0, 255, 144)">Roster: </div>
<span id="teamRoster"></span>
<br />

Which appears like Roster: in the View

Right now my snippet has been modified to add names:

var rosterListings = "";
for (var i = 0; i < teamRoster.length; i++) {
    rosterListings = rosterListings + teamRoster[i] + ", ";
}
$("#teamRoster").text(rosterListings);

Which will update my View to Roster: John, Frank, Susan, ect..

However, I am trying to now add <a href> tag's around each person and turn them all into actual links. My attempt looks like this

var rosterListings = "";
    for (var i = 0; i < teamRoster.length; i++) {
        rosterListings = rosterListings + " <a href='" + idList[i] + "'>" + teamRoster[i] + "</a>,";
    }
    $("#teamRoster").text(rosterListings);

which displays as

Roster: <a href='#'>John</a>, <a href='#'>Frank</a>, ect..

I understand why this occurring since I am setting actual text/strings, but is there a way to convert this string into HTML elements? I have tried a few $.parseHTML code snippets that I found from Googling but I must be implementing them all wrong.. :(

Any help appreciated, Thank you!

like image 436
Austin Avatar asked Jul 09 '14 15:07

Austin


People also ask

How do I turn a string into a hyperlink?

The easiest way to do this is to use a button with type "Link" and set the string attribute as the URL attribute. If your use case is a bit more complicated, for example, when you need to display the link as part of a text, things like the "Rich-Text" Widget might help.

How do you convert a string in HTML?

The simplest way to do this is to create an element, insert the string into with innerHTML , then return the element. /** * Convert a template string into HTML DOM nodes * @param {String} str The template string * @return {Node} The template HTML */ var stringToHTML = function (str) { var dom = document.

How do I convert a string to an integer in HTML?

syntax. parseInt(value); This function takes a string and converts it into an integer. If there is no integer present in the string, NaN will be the output.

How do I parse a dom in HTML?

Another way of converting HTML to DOM is to use the traditional document. createElement method. After creating the element, the HTML string can be assigned to the innerHTML attribute of the element and the JS engine would parse it to the child elements of the created element.


1 Answers

Well, solution is quite obvious

Just replace

 $("#teamRoster").text(rosterListings); 

With:

$("#teamRoster").html(rosterListings);

Because if you use it as a text then it will treat it as the text and if you write html then it will treat it as a html

like image 91
Just code Avatar answered Oct 14 '22 05:10

Just code