2

I have an append statement and I am trying to pass a javascript variable for the ahref attribute.But it is getting rendered as text.I want to know how do i pass the variable as a variable and not text in this append statement

jQuery('#sidr-main').append('<a class="navlink level0 addedMenu" href="abc"><li class="navlist level0">some link</li></a>');

abc is a js variable which i declared at another place and i want the value to be passed in the DOM as a variable and not the text there is no link called abc

1 Answer 1

5
jQuery('#sidr-main').append('<a class="navlink level0 addedMenu" href="' + encodeURIComponent(abc) + '"><li class="navlist level0">some link</li></a>');

Make sure you do encodeURIComponent :)

In ES6 you could also use templated literal to get something more cleaner:

var str = `<a class="navlink level0 addedMenu" href="${encodeURIComponent(abc)}"><li class="navlist level0">some link</li></a>`
jQuery('#sidr-main').append(str)
Sign up to request clarification or add additional context in comments.

3 Comments

Great! If this is what you needed you should mark the answer so others can find it easier etc.
i upvoted it but i dont have enough reputations for it to show up :(
np - no worries at all :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.