user1953875
user1953875

Reputation: 29

Using $('selector').each() to modify and update html of DOM elements?

I am trying to create an event listener across a bunch of links on my site. These links are generated within a loop, so I end up with <a class = "replyButton" id = "replyID"<? echo $x; ?> etc.

I'm trying to use the code below to reveal an input box when each respective link is clicked, but with no luck. I can get it to work using plain JS too, in one case, but not using JQuery, extrapolated across several like this. Any help would be really awesome.

window.onload = function(){

    $('.replyButton').each(function(index){
    var domElementId = "replyArea" + index;
    domElementId.onclick = function() {
    var replyFieldHtml = '<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>';  
    document.getElementById('domElementId').innerHTML = replyFieldHtml;
    console.log('domElementId');
    return false;
    }
}); 
}

Edit: here is the loop im using to generate the html... $x = 0; while ($x < 8){ $x++; $r = $wallarray - $x;

$postContent = $wall_content['wall_posts'][$x-1];
$postUser = getUserNameById($wall_content['userID'][$x-1]);
?>

<div class = "row">
    <div class = "span6">
        <div class = "span1" id = "wallPhoto"><img src ="assets/img/usr/profile_holder.jpg></div>
        <div class = "span4">
            <div class = "span4" id = "wallFeedStyle"><a id = "wallUserLink" href = "#"><b><? echo $postUser; ?></b></a></div>
        <div class = "row">
            <div class = "span5">
                <div class = "span4" id = "userPost"><? echo $postContent; ?></br><p class = "wallsmall"><a href="#" id = "postLike"></i>Like</a> &middot;<a class = "replyButton" id = "replyButton<? echo $x; ?>" href="#"></i>Reply</a></p></div></div>
            </div>
            <div class = "row">
                <div class = "span5">
            </div>
        </div>  
        <div class = "row" id = "replyArea<? echo $x; ?>"></div>
</div>
<? 
}
?>

Upvotes: 2

Views: 189

Answers (2)

user1953875
user1953875

Reputation: 29

I ended up using the code below to solve this one, after digging deeper into the history behind .on() and .bind(). Thanks for all your help!

$('a.replyButton').on("click", function(){
var index = $(this).attr('id');

$('#replyArea'+index).html('<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>');
});

I ended up changing the "replyLink" ID attribute to just the number. So there were a bunch of /<.a> with class replyButton, and ID attributes as a number. And it seems to do the job nicely, no need to setup the .each() loop too.

Upvotes: 0

hjpotter92
hjpotter92

Reputation: 80639

You're using the variable in the wrong manner. Try this:

window.onload = function () {
    $('.replyButton').each(function (index) {
        var domElementId = "replyArea" + index;
        $('#' + domElementId).on('click', function () {
            var replyFieldHtml = '<div class = "span4" id = "replyTextField"><table><tr><td id = "replyPhoto"><img src = "/assets/img/usr/profile_holder.jpg" /></td><td id = "replyContent"><input type = "text" id = "replyInput" onkeydown="if (event.keyCode == 13) wallReply()" placeholder = "leave a reply..." /></td></tr></table></div>';
            $(this).html(replyFieldHtml);
            console.log(domElementId);
            return false;
        });
    });
}

Upvotes: 4

Related Questions