Javascript: Add HTML to string - make a link and add it to output -
new javascript, how fix problem i'm having?
i want add a tag string variable i'm creating using function.
the whole message (eg - "your tutor john doe. contact them here: contact form") added dom function (which works).
when run script outputs browser second part (makelink()) doesnt work.
this get: "your tutor john doe. contact them here http://www.example.com" - instead of url want word contact form should link.
how do this?
i tried using link() method, didnt work , had similar output.
i'll include relevant script below, rest works fine...
function makemessage(){ for(i in msgarr){ stringmsg += msgarr[i]; //do not add comma after last item if(i < msgarr.length - 1){ stringmsg += ', '; } } var highriskmsg = "your tutor " + stringmsg + ". contact them here" + makelink(); return highriskmsg; } function makelink() { var contactlink = document.createelement("a");//create <a> tag contactlink.setattribute("id", "linkc");//set id att <a> tag contactlink.setattribute("href", "http://www.example.com/contact.php");//set id att <a> tag var contactlinktxt = document.createtextnode("contact form");//new text node contactlink.appendchild(contactlinktxt);//append text child of <a> tag return contactlink; }
it seems problem returning dom element makelink()
function, , won't concat string expect.
you need return valid html string instead, such as: <a id=".." href="..">..</a>
the quickest way fix code change return makelink()
function follows:
return contactlink.outerhtml;
using outerhtml
return html string element, rather element itself.
Comments
Post a Comment