3

Is is possible to assign a data value to the element below - and use the numbers in the string as the value with jQuery/Vanilla JS?

I want this:

<div class="priceinfo col5">2560kr</div>

To look like this:

<div class="priceinfo col5" data-price="2560">2560kr</div>

The value is dynamic - the class of the element is the same.

2 Answers 2

8

This can be done with the jQuery attr() method:

var someValue = 2560;
$('.priceinfo.col5').attr('data-price', someValue);

You can set any attribute, including custom attributes such as HTML5 data- attributes, using attr().


You can also pass a function instead of a fixed value to .attr():

$('.priceinfo.col5').attr('data-price', function() {
    var text = $(this).text();
    return parseInt(text, 10); //drops the non-numeric characters at the end of the text
});

This is extremely useful when there are multiple elements in the jQuery set -- multiple elements with the classes priceinfo and col5.


If the value might sometimes have initial non-numeric characters, then you could use a regular expression to parse the text:

$('.priceinfo.col5').attr('data-price', function() {
    var text = $(this).text();
    var matches = /\d+/.exec(text);
    if (!matches) {return '';}
    return parseInt(matches[0], 10);
});
Sign up to request clarification or add additional context in comments.

9 Comments

Hi! I would like to create the data value from the string in the element. Since this is a dynamic number i cant set a value myself with the script.
@Zev Spitz I just realized a situation where I will have a non-numeric character before the numbers in the string- is it possible to account for that as well?
@Zev Spitz thanks - that worked great. I noticed two other situations that made the script not working - in one instance I had a comma in the middle. this made the data price only to be the number before the comma. the second instance there where one of these repeating elements that only had text - no numbers. the element that only had text made the script not work on the elements with numbers inside it. the first one I can avoid, no commas. but the second I cant. Is it any way to account for that?
@JerrySvensson RE commas, you can make the regular expression more complex, to account for commas and decimal separators, or you can find a Javascript library that parses text into numbers while taking into account number formats. RE empty text -- Can empty values be treated as 0?
@Zev Spitz Seems like it cant be treated as 0 - you last snippet gave me this: TypeError: Cannot read property '0' of null
|
8

You can do that with plain javascript as well:

function setAttribute(selector, attribute, value) {
    var elements = document.querySelectorAll(selector);
    for (var index = 0; index < elements.length; index++) {
        elements[index].setAttribute(attribute, (typeof value === "function" ? value(elements[index]) : value));
    }
}

Comments

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.