0

I have a div tag:

<div id="content"><p></p><a href="#"></a></div>

I want to insert dynamically different content into the <p> tag and also into the href attribute of the <a> tag.

I am using jQuery.

2

1 Answer 1

4

As easy as:

$("#content > p").html("Some content");
$("#content > a").prop("href", "/new/link.html");

Or in chain:

$("#content")
  .find("p").html("Some content")
    .end()
      .find("a").prop("href", "/new/link.html");
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! If I have 2 tags like <a href="#" class="blue one"></a><a href="#" class="red one"></a> how can I change only one of them?
@user550413 You class selectors: $("#content a.blue").prop(...);.
I tried it but because my class has spaces in it's name (I use it in css) it doesn't work.
@user550413 Class name can't have spaces. Spaces split different classes set to the element, i.e. class="blue red" means that element has both CSS classes blue and red.
I figured it out. Many Thanks!

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.