mmcsacc mmcsacc - 3 months ago 8
HTML Question

How do I return the value of the class <div> without returning the value of the <span>

How do I return the value of "Jhon Doe" without the "x"

https://jsfiddle.net/sok1hkkt/3/

<div class="chip"><span class="closebtn" onclick="this.parentElement.remove()">&times;</span>John Doe</div>
<script>
var array = $( ".chip" )
.map(function() {
return this.textContent; //changeing only this line
})
.get()
.join();
alert(array)
</script>

Answer

You can do it like that:

// Take the whole inner HTML, split it at the span closing tag,
// take the second element which is the desired text
var text= $( ".chip" ).html().split('</span>')[1];
alert(text);

https://jsfiddle.net/sok1hkkt/4/

If you need it for multiple elements, you can use the jQuery .each() function like this:

var array = [];

$( ".chip" ).each(function(){
    array.push($(this).html().split('</span>')[1]);
});

alert(array);

https://jsfiddle.net/sok1hkkt/7/