Reputation: 1032
<div id="qselected">
<div class="qitem di ui-draggable ui-draggable-handle">
2.A computational agent is considered intelligent if it can adapt its actions to a particular setting.
</div>
<div class="qitem di ui-draggable ui-draggable-handle">
4.A computational agent is considered intelligent if it can adapt its actions to a particular setting.
</div>
</div>
ans5 = $('#qselected div').map(function(){
return this.class;
});
for(i=0; i < ans5.length; i++)
alert(ans5[i]);
How do I get text from div id "qselected" and within the class "qitem" . I want to story all texts inside div's and div's.From the array arr[0] to 2.A computational agent is considered intelligent if it can adapt its actions to a particular setting.similarly for arr[1] to 4.A computational agent is considered intelligent if it can adapt its actions to a particular setting.Help me to get out of this issue.Thanks in advance.
Upvotes: 0
Views: 1107
Reputation:
If you wish to have it done without using jquery
var arr = Array.prototype.slice.call(document.querySelectorAll('.qitem')).map(function(a){return a.textContent});
Upvotes: 1
Reputation: 133403
Problem in your code is that you are not accessing the correct property. there is no property as class
thus it must be returning undefined
You need to return textContent
property in the .map()
along with .get()
to get basic array.
var arr = $('#qselected .qitem').map(function(key, item){
return this.textContent.trim(); //$(this).text().trim();
}).get();
console.log(arr)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="qselected">
<div class="qitem di ui-draggable ui-draggable-handle">
2.A computational agent is considered intelligent if it can adapt its actions to a particular setting.
</div>
<div class="qitem di ui-draggable ui-draggable-handle">
4.A computational agent is considered intelligent if it can adapt its actions to a particular setting.
</div>
</div>
Upvotes: 2