admin管理员组文章数量:1426224
Example here:
how can i display the text of the green div in the grey div, when i click on the [+] icon? Ive tried many different scenarios but none works, can someone give me few pointers how can i do this, i would really appreciate.
<div id="wrapper">
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
this is the 1st element
</div>
<div class="first" id="s">
<div class="set">+</div>
this is the 2nd element
</div>
<div class="first" id="t">
<div class="set">+</div>
this is the 3rd element
</div>
</div>
<div id="cc"></div>
</div>
CSS
.first{
margin-top:5px;
border:1px solid black;
width:190px;
height:40px;
background-color:green;
}
#cc{
margin-top:5px;
width:190px;
height:40px;
border:1px solid black;
background-color:grey;
}
.set{
cursor:pointer;
color:#fff;
font-size:33px;
float:right;
border:1px solid white;
}
Example here: http://codepen.io/agentmi6/pen/JoZZWm
how can i display the text of the green div in the grey div, when i click on the [+] icon? Ive tried many different scenarios but none works, can someone give me few pointers how can i do this, i would really appreciate.
<div id="wrapper">
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
this is the 1st element
</div>
<div class="first" id="s">
<div class="set">+</div>
this is the 2nd element
</div>
<div class="first" id="t">
<div class="set">+</div>
this is the 3rd element
</div>
</div>
<div id="cc"></div>
</div>
CSS
.first{
margin-top:5px;
border:1px solid black;
width:190px;
height:40px;
background-color:green;
}
#cc{
margin-top:5px;
width:190px;
height:40px;
border:1px solid black;
background-color:grey;
}
.set{
cursor:pointer;
color:#fff;
font-size:33px;
float:right;
border:1px solid white;
}
Share
Improve this question
asked Feb 28, 2015 at 13:39
johnjohn
8341 gold badge14 silver badges35 bronze badges
1
- 1 Thanks for all your solutions, i tried all of them, and thanks for your time and fast response. and damn i cant upvote still low rep user. – john Commented Feb 28, 2015 at 14:24
6 Answers
Reset to default 1You'll have to get the text of the .first div, i suggest you put the text into a tag so it'll be easy to get it, and so you don't get the + of the .get button. Here's the new JavaScript
$(document).ready(function(){
$('.set').click(function(){
// Get the text inside the span in the parent .first of the clicked .set button
var text = $(this).parent('.first').find('span').text();
$('#cc').text(text);
});
});
The HTML would look like this
<div class="container">
<div class="first" id="f">
<div class="set">+</div>
<span>this is the 1st element</span>
</div>
<div class="first" id="s">
<div class="set">+</div>
<span>this is the 2nd element</span>
</div>
<div class="first" id="t">
<div class="set">+</div>
<span>this is the 3rd element</span>
</div>
</div>
<div id="cc"></div>
Here's an updated CodePen
You can use this:
$(document).ready(function(){
$('.set').click(function(){
var text = $(this).parents("div.first").text();
$("#cc").text(text);
})
});
This Code could help you
var tempText="";
$('.container .set').each(function(){
tempText=tempText+ $(this).html();
});
$('#cc').html(tempText);
Here try this (fiddle: http://jsfiddle/nek9fona/)
JQ:
$(function(){
$('.set').click(function(){
var $this = $(this);
var text = $this.parent('.first').text();
$('#cc').text(text);
});
});
Put this in your head html section. Or bottom of body section.
<script>
$(document).ready(function() {
$('.set').on('click', function(){
var text_val = $(this).closest('div.first').text();
$('#cc').text(text_val);
});
});
</script>
I guess that you don't want the copied text to contain "+" symbol? In this case you can do this:
$('.set').click(function() {
var text = $(this.nextSibling).text().trim();
$('#cc').text(text);
});
Demo: http://codepen.io/anon/pen/MYXXrr
Note, that it makes sense to improve HTML structure a little by wrapping the text into some container:
<div class="first" id="s">
<div class="set">+</div>
<div class="text">this is the 2nd element</div>
</div>
In this case, code would bee much more reliable and cleaner:
$('.set').click(function() {
var text = $(this).next('.text').text(); // or $(this).parent().find('.text')
$('#cc').text(text);
});
Demo: http://codepen.io/anon/pen/ogyyoO
本文标签:
版权声明:本文标题:javascript - How to get the text value from divs with same class, and to display in another div? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745428471a2658220.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论