4

I'm trying to reproduce this html using jquery on a click event.

<div class="comments_action_207">
   <div class="list_item_comments list_item_comments_207">
      <div class="comment_separator">text goes here</div>
   </div>
</div>

before the click event I have the following html:

<div class="comments_action_207"></div>

So I need to add this inside the previous code

<div class="list_item_comments list_item_comments_207"><div class="comment_separator">text goes here</div></div>

3 Answers 3

2

I assume you're looking for something similar to this:

$(function() {
    $('.comments_action_207').click(function() {
        var num = this.className.split('_').pop();
        $('<div/>',{'class':'list_item_comments list_item_comments_' + num})
            .append('<div class="comment_separator">text goes here</div>')
            .appendTo(this);
    });
});

The click is assigned specifically to the comments_action_207 element. I assume you want to assign to others as well.

This will take the number from the end of the class, create the new elements, adding on the number where needed, and will append the result to the comments_action_207 element.

The resulting HTML will be:

<div class="comments_action_207">
    <div class="list_item_comments list_item_comments_207">
        <div class="comment_separator">text goes here</div>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

2 Comments

I've got this (dont need the click event and num) $('<div/>',{'class':'list_item_comments list_item_comments_214'}) .append('<div class="comment_separator">test</div>') .appendTo(this); but dont work
@Yannick - Are you saying that you're not in a click handler? What is triggering the code? If you're not in a handler, but it is just running when the page loads, change .appendTo(this) to .appendTo('.comments_action_214').
1

you want to set the html code you want to add as a string datatype in a variable in jquery. then in your click function handler, set the html content of the div to the variable. in jquery it would look something like this. (assuming jquery is loaded before this script is ran).

http://docs.jquery.com/Html documentation for how to get and set html in selectors with jquery.

//jquery code:

<script type="text/javascript">
$(document).ready(function(){
    var stringToAdd = "<div class='list_item_comments list_item_comments_207'><div class='comment_separator'>text goes here</div></div>"

    $('.comments_action_207').click(function(){
       $(this).html(stringToAdd);
     });

});
</script>

2 Comments

Make code monospace (and therefore legible) by enclosing it in backticks ` or indenting it by four spaces. Great answer, though!
Sorry I'm new to Stack Overflow so I just typed the code with manual tabbing
0
$(".comments_action_207").empty(); // clear the div off, if you do not want to retain old values

$(".comments_action_207").append("<div class='list_item_comments list_item_comments_207'><div class='comment_separator'>text goes here</div></div>");

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.