Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attrOrDefault method #215

Closed
wants to merge 1 commit into from
Closed

Conversation

SuprDewd
Copy link

A little helper method for returning a default value if the specified attribute isn't set.

For example, if we have a slider:

And we would like to get the min, max and step attributes,
and provide default values for them if they aren't set,
we would do something like the following:
var slider = $('#slider'),
min = slider.attr('min'),
max = slider.attr('max'),
step = slider.attr('step');

if (min === undefined) min = 0;
if (max === undefined) max = 100;
if (step === undefined) step = 1;

But with the attrOrDefault method, we can do the following:
var slider = $('#slider'),
min = slider.attr('min', 0),
max = slider.attr('max', 100),
step = slider.attr('step', 1);

Unit tests are also included.

@SuprDewd
Copy link
Author

We can also do:

var slider = $('#slider'),
    min = slider.attr('min') || 0,
    max = slider.attr('max') || 100,
    step = slider.attr('step') || 1;

Which makes this commit useless...

@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant