Henrik Petterson Henrik Petterson - 5 months ago 9x
Javascript Question

.split() a data attribute isn't working when it should

I want to

.split(" ")
a data attribute value but it returns empty:

var bulletcontent = jQuery(this).closest("div").find("p").attr('data-bcontent');

// split content into words
var words = jQuery(bulletcontent).text().split(" ");

Why isn't this working?

Here is a jsFiddle.


See the fiddle: https://jsfiddle.net/43439o8o/2/

You dont need to get the .text(), the .attr() already return his content:

var bulletcontent = jQuery(content).attr('data-bcontent');

// split content into words
var words = bulletcontent.split(" "); //Remove the .text() method

As you can see at the .attr() method docs, you can use it to return or set the attribute value:

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.