I am trying to make a add clicked element data text inside other div using jquery.
Its operating logic should be as follows:
When you click word1 then this clicked word1 should be come in #words div after added the word1 if you want to add word2 also then click word2 it should be come in #words div also.
My problem is I can not add second clicked word.
Here is the DEMO from jsfiddle.
$("body").on("click", ".word", function() {
var dataWord = $(this).attr("data-word");
var dataBox = $("#words");
var oldWords = $(dataBox).html("<div class='ab'>"+dataWord+"</div>");
$(dataBox).html(oldWords.html() + oldWords.html());
return false;
});
html, body {
width: 100%;
height: 100%;
padding: 0px;
margin: 0px;
background-color: #FAFAFA;
font-family: 'Helvetica Neue', helvetica, arial, sans-serif;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
-ms-text-size-adjust: 100%;
-webkit-texts-size-adjust: 100%;
-webkit-backface-visibility: hidden;
}
.container {
position:relative;
width:100%;
max-width:400px;
margin:0px auto;
padding:50px 0px;
overflow:hidden;
}
.word {
position:relative;
background-color:red;
border-radius:2px;
margin-left:5px;
color:#ffffff;
float:left;
font-weight:400;
font-size:13px;
padding:5px;
cursor:pointer;
}
.word:hover {
background-color:black;
color:#ffffff;
}
.addedWords {
position:relative;
float:left;
width:100%;
padding:10px;
background-color:#d8dbdf;
margin-top:40px;
}
.ab {
background-color:blue;
color:#ffffff;
margin-left:5px;
float:left;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<div class="word" data-word="word1">
Word1
</div>
<div class="word" data-word="word2">
Word 2
</div>
<div class="addedWords" id="words">
</div>
</div>