0

I am trying to create a href with html(), however the output is not correct. What is wrong with this jquery code?

$('#tag_modal').find('.modal-body').html("<a href='/companies/explore-companies?tags='"+data.results['tag_id']+">test</a>");

Output:

<div class="modal-body"><a 5="" href="/companies/explore-companies?tags=">test</a></div>
2
  • are you missing a ' in ">test</a>" ? Commented Aug 5, 2015 at 0:29
  • 2
    @Paul it's there, just in wrong place Commented Aug 5, 2015 at 0:30

3 Answers 3

4

You're closing the attribute before the tag id, close it after

$('#tag_modal').find('.modal-body').html("<a href='/companies/explore-companies?tags="+data.results['tag_id']+"'>test</a>");
Sign up to request clarification or add additional context in comments.

Comments

3

Your quotes are wrong. Try:

$('#tag_modal').find('.modal-body')
    .html('<a href="/companies/explore-companies?tags='+data.results['tag_id']+'">test</a>');

Comments

0

Try creating your anchor element first:

var $anchor = $("<a href='/companies/explore-companies?tags='"+data.results['tag_id']+"'>test</a>");

And then appending it to your div:

$('#tag_modal').find('.modal-body').append($anchor);

That way you're breaking the process into discrete steps (first create the element, second append it to the DOM).

Edit: as others have mentioned, you also need to close the href value, with a single quote, before you close the tag with the greater-than symbol.

Comments

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.