Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Kristian Woods
23,414 PointsHow can I add custom key/values on the fly in my script?
I'm trying to create a JS plugin that will generate certain attributes for accessibility purposes. I'm trying to create it with as much flexibility as possible. Right now, I'm trying to add functionality so that you can add custom attributes on the fly when you call the 'dropDown' plugin function. I'm not sure what is the best way to approach it, though.
(function ( $ ) {
$.fn.dropDown = function(props) {
/***********************
'props' is the object passed to the function call. You can then create multiple key/value pairs.
In this case, we have the parent of the dropdown, the dropdown content, and the close/open text, which will be
appended to the aria-label
************************/
var element = this;
// set atts for element
$(element).attr({
"tabindex": "0",
"role": "button",
"aria-expanded": "false",
"aria-pressed": "false",
"data-closed-text": props.closedText,
"data-open-text": props.openText,
"aria-label": props.closedText
});
// set content to be focusable on keyboard
$(element).next(props.dropdown).attr({
"tabindex": "0"
});
// set above atts on click, keypress events
$(element).on('click keypress', function() {
if((element).attr('aria-pressed') === "false") {
$(element).attr({
'aria-pressed': 'true',
'aria-expanded': 'true',
'aria-label': props.openText
});
$(element).parents(props.parent).find(props.dropdown).css('height', "400px");
} else {
$(element).attr({
'aria-pressed': 'false',
'aria-expanded': 'false',
'aria-label': props.closedText
});
$(element).parents(props.parent).find(props.dropdown).css('height', "");
}
});
};
}( jQuery ));
$('.container').dropDown({
"closedText": "drop-down collapsed", // define closed text for aria-label
"openText": "drop-down expanded", // define open text for aria-label
"parent": ".book-now", // select parent container for context
"dropdown": ".content" // select dropdown element
});
1 Answer
Michael Cook
Full Stack JavaScript Techdegree Graduate 28,975 PointsYou have included a fairly sizable chunk of code, but the question you're asking is a bit vague and difficult to pinpoint. Just what exactly do you need help with? Be as specific as possible! I looked at your codepen and I am still having difficulty discerning just exactly what it is you are trying to do. I'm sure that once it becomes clear exactly what you're trying to achieve, others will be able to give you an answer :).