0

How can I extract the string from a certain data attribute via regexp.

<button data-loading-text="Some text on loading" data-toggle="button">Button</button>
<button data-finished-text="Some text on finished" data-toggle="button">Button</button>

And my javascript is

var Buttons = document.querySelectorAll([data-toggle="button"]);
[].forEach.call(Buttons, function (item) {
    var data = item.getAttribute('data-'+INEEDTHIS+'-text')
    var option = INEEDTHIS
    return new Button(item,option);
})

2 Answers 2

1

You can use the Element.attributes property:

var attrs = item.attributes;
for(var i = attrs.length - 1; i >= 0; i--) {
    var m = attrs[i].name.match(/^data-(.*)-text$/);
    if (m) {
        var option = m[1];
        // do something
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the .attr() method of jQuery

1 Comment

I know jQuery cand do attr(), I need native Javascript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.