Ankita Kashyap Ankita Kashyap - 1 month ago 8
HTML Question

Javascript dynamic creatationg of anchor tag is not working

i am trying to create html with JavaScript. but my link to anchor tag is not working as expected.

var test ="http://blogs.edweek.org/edweek/finding_common_ground/2016/10/if_coaching_is_so_powerful_why_are't_principals_being_coached.html?cmp=SOC-EDIT-FB";

var res ="<a style='color:#7Bc76B;text-decoration: none;' target='_blank' href='"+test+"'>Link to read more ></a>";

document.getElementById("res").innerHTML = res;


https://jsfiddle.net/b4kqsn6u/

Any help will be appreciated.

Thanks

Answer

You can simply modify your <a> declaration as follows to take care of single quote present in your URL:

var res ='<a style="color:#7Bc76B;text-decoration: none;" target="_blank" href="'+test+'">Link to read more ></a>';

declared within single quote as above and all attributes are within double quotes

https://jsfiddle.net/b4kqsn6u/3/

Comments