Anson Aştepta
Anson Aştepta

Reputation: 1143

Assign class for .map() values

For example I have a function like below:

OrSovSKeyword = $('.OrsovWord').map(function() {
    // get text 
     return $(this).text()
    // get result as array
).get();

I'd like to assign class for each result it generates, therefore I used this:

 OrSovSKeyword = $('.OrsovWord').map(function() {
    // get text 
     return <span class="123"> $(this).text() </span>
    // get result as array
).get();

However this returns something like below in my code:

enter image description here

As you can see there's two \ in between makes me can access the element by getting it's class, how can I fix it?

Update:

var sovName = $("#input_SovName_Adv").val();
var sovSKeyword = "";

OrSovSKeyword = $('.OrsovWord').map(function() {
    return  '<span class="Or">' + $(this).text() + '</span>'
}).get();

AndSovSKeyword = $('.AndsovWord').map(function() {
    return  '<span class="And">' + $(this).text() + '</span>'
}).get();

$("#insertedNewSov").append('<tr>' +
    '<td class="sovNames">' + sovName + '</td>' +
    '<td class="sovKeywordArr">' + JSON.stringify(AndSovSKeyword).replace(/\[/g,'').replace(/\]/g,'').replace(/\"/g,'') + ',' +  JSON.stringify(OrSovSKeyword).replace(/\[/g,'').replace(/\]/g,'').replace(/\"/g,'') + '<p class="right"><i class="fa fa-times removeRow" aria-hidden="true" style="color: red;"></i></p></td>' +
'</tr>');           

Upvotes: 0

Views: 38

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337646

The problem is because you're using JSON.stringify on your arrays before appending them to the DOM - hence the odd formatting. You need to join() them together instead. Try this:

$("#insertedNewSov").append('<tr>' +
    '<td class="sovNames">' + sovName + '</td>' +
    '<td class="sovKeywordArr">' + AndSovSKeyword.join('') + ',' + OrSovSKeyword.join('') + '<p class="right"><i class="fa fa-times removeRow" aria-hidden="true" style="color: red;"></i></p></td>' +
'</tr>');

Upvotes: 3

Related Questions