Day of week selector

The Web Content Accessibility Guidelines were developed through a Milestones process by W3C in cooperation with individuals and organizations around the world.

They have published a set of standards that all public serving institutions must follow when providing content to users via the internet.

At Skagit Valley College I was asked by the Disability Coordinator to come up with a solution for selecting the days of the week from a control on a page of the web application.

I came up with this day of week selector that lets the user choose the days that they need. I am able to use the standard labels and ID names to enable screen readers and other accessibility software to describe the content to the user in a beneficial manner that they could easily understand and interact with.

The ui is just an unordered list with the days of week as id.

<label for=”weekdays”>Day(s)</label>
<ul id=”weekdays”>
<li><a id=”Monday”>M</a></li>
<li><a id=”Tuesday”>T</a></li>
<li><a id=”Wednesday”>W</a></li>
<li><a id=”Thursday”>T</a></li>
<li><a id=”Friday”>F</a></li>
</ul>

Then I added the ability to select them by manipulating the class with jquery using a click event handler…

// make a days of week choice selector
$(“#weekdays a”).on(“click”, function(event){
$( this ).toggleClass(“success”);

});

Then, to gather the info and submit with the form, I select the li’s with the class identifier, and add them to a form variable as so…

var weekdays=$(“.success”);
var the_weekdays=””;
weekdays.each(function(){
if(the_weekdays!==””){the_weekdays=the_weekdays+” , “;}
the_weekdays=the_weekdays+$( this ).attr(“id”);
});
$(“input[name=the_weekdays]”).val(the_weekdays);

I have even thrown in some code to put a comma between multiple days if selected. The string that is submitted with the form  would look like this: Tuesday,  Thursday

build an object, and insert into dom using javascript and jquery

var div = document.getElementById("yourDivElement");
var input = document.createElement("textarea");// this isgreat if you don't need to put contents into the text area dynamically.an empty text area.
var button = document.createElement("button");
input.name = "post";
input.maxLength = "5000";
input.cols = "80";
input.rows = "40";
div.appendChild(input); //appendChild
div.appendChild(button);

$('<tr>').append(
$('<td>').append(
$('<a>', {
href: o.link,
text: o.word,
onclick: function() {someFunc(o.string);}
})     )

 

I have a list that I need to insert an new anchor with javascript(because you should always use native javascript when possible), and build its attributes using jquery.

To create the new anchor :

var myNewTag = document.createElement('a');

var mynewthingy=document.createElement(‘a’);
mynewthingy.setAttribute(“href”, “http://dwdenney.com”);
mynewcategory.innerText=”visit deedubs”;
$(“#mydomobject”).append(“<li id=’atempnameremovewhendone’></li>”);
var atempnameremovewhendone = document.getElementById(“atempnameremovewhendone”);
atempnameremovewhendone.appendChild(mynewthingy);

Using Ajax loading animations.

There are some techniques for making the “Loading” animation work when you want it.

During your jquery script calls, you used to initiate the loading anim using:

$.mobile.showPageLoadingMsg(“b”, “Attempting login…”);

The docs say that the mobile pageloading is depreciated and to use this:

$.mobile.loading( 'show') ;

Here is how to customize the call to change the theme, what text is displayed, and whether or not the spinning loader graphic is displayed.

$.mobile.loading( 'show', { theme: "b", text: "foo", textonly: true });
Object Param Arguments
theme (string, default: “a”) The theme swatch for the message.
text (string, default: “loading”) The text of the message.
textonly (boolean, default: false) If true, the “spinner” image will be hidden when the message is shown.
textVisible (boolean, default: false) If true, the text value will be used under the spinner.
html (string, default: “”) If this is set to a non empty string value, it will be used to replace the entirety of the loader’s inner html.
Here is my final version of the loader used in my progress reporting application. I created a custom animation using the school’s logo and scripted the loading animation whenever user interaction required a notice that things were going on behind the scenes.
$.mobile.loading( ‘show’, {
text: ‘Accessing Data…’,
textVisible: true,
theme: ‘b’,
html: “<center>Attempting Login…<br/><img src=’themes/images/svcloader_sm.gif’></center>”
});
I love using the “depreciated” center tag because it works so well on EVERY device and browser that exists. This is one of those rare times when CSS isn’t the answer, since I don’t’ want to make a special style sheet that would need to be included in every situation that I call this from.
When my scripts are done with the user interaction, I close the loading animation as such:
$.mobile.loading( ‘hide’ );

Formatting currency fields using javascript

This actually uses jquery, but yo could replace the jquery with doc.getelem….

Here is a sick way to get rid of $ signs, commas and anything after decimal point.

for example: $45,000.45 becomes a clean number 45000 ready for calculations.

No, it doesnt round. just removes the change.

// strip out any $$ .00 and anything else
function stripper(who){
dirtynumber = $( who ).val();
cleannumber = parseInt(dirtynumber.replace(/[^d.]/g, “”));
$( who ).val(cleannumber);
}

Handling Events With J Query

jQuery event.target always refers to the element that triggered the event, where 'event' is the parameter passes to the function. http://docs.jquery.com/Events_(Guide)

$(document).ready(function() {     $("a").click(function(event) {         alert(event.target.id);     }); });
If the  <a> that is clicked were a game element, such as an ace card, the card could tell you "I am an ace of spades" for your code to do what ever.
$( "p" )
.html( newText )
.find( "span" )
.hover(function() {
$( this ).addClass( "hilite" );
}, function() {
$( this ).removeClass( "hilite" );
})
.end()
.find( ":contains('t')" )
.css({
"font-style": "italic",
"font-weight": "bolder"
});

Setting an event handler is not too difficult, but what about removing the event. I don’t want the user to be able to hit the button more than once and still fire the function. the only way that was working was to disable the button or hide it.  I finally found the technique that really works for me. The prop() is a powerful function with untold uses. Dealing with checkboxes seems to be something that would merit its own post. http://api.jquery.com/prop/

Here is a great way to remove a click event that you put inline:

<A href=”#” onclick=”mycoolfunk()” id=”thisbut”>my button or link</a>

$(‘#thisbut’).prop(“onclick”, null);

 

Update: I have been fighting with finding the best way to remove a click event from the button so the user cant hammer on it repeatedly. None of my solutions were working effectively in all situations: like actions on  a submit button are harder to remove than ones on a link.I couldn’t get the submit to submit to my will. I wanted to hide it onclick, not happenin. remove click event, not workin either. What the hell should I do?

Well, enter the .one()  — thats right. http://api.jquery.com/one/ The jquery docs have a function to only put one click event onto an object. This is perfect for a submit where we don’t want the user to be able to repeatedly press the figgin button cause they think everything should be so instantaneous. LOL.

 

But what about handling multiple events.  I have been looking for this for years… We have the  $( document ).on( "click", "#demo-page", function( e ) {alert(e.type)}); alerts(‘click’)

Well here’s how to handle multiple events with one on call:

 $( document ).on( "click press swipeleft swiperight", "#demo-page", function( e ){
alert(e.type); 
if ( e.type === "swipeleft"  ) {// do something only on swipeleft}
});// end doc.on funk

 

I will write up a demo page with this script so I can test it out and do stuff with events.

 

Using delegated events to allow dynamic elements to have the event handler.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Lets take for example the diploma application I am working on. I have some buttons that have styles and actions that need to be called, but the buttons are not in the dom until and unless the user accesses that particular step of the application. How do I assign the events and styles to non-existing buttons. I had tried a few various solutions before I read about this. Now lets go for this approach. I will delegate the event to the form element. That way, any dynamically added elements will still get the mouseover and mouseout effects.

$(“#diplomappform”).on(“mouseover mouseout”, “[name=’stepUpButton’]”, function(e){
if(e.type==”mouseover”){
$(this).removeClass(“blackouterstrokesmall”).addClass(“boldfont blackouterstrokemedium insetborder whitetextglowtight”);
}
if(e.type==”mouseout”){
$(this).removeClass(“boldfont blackouterstrokemedium insetborder whitetextglowtight”).addClass(“blackouterstrokesmall”);
}
});

 

Sometimes if  you have a mouse over event, the mouse out event will trigger accidently too early. Peter has worked out a fix for this: make a function to call the mouse over function instead of putting an anonymous function directly into the event.
var qlinkTrigger = function() {
$(“#qlink-box”).slideDown(“slow”, function() {
$(this).clearQueue();
});
}

$(“a#qlink-trigger”).hover(qlinkTrigger).click(qlinkTrigger);

$(“#qlink-box”).mouseleave(function() {
$(this).slideUp(“slow”);
});

$(“a.close-qlinkbox”).click(function() {
$(“#qlink-box”).slideUp(“slow”);
return false;
});

Using custom attributes in jquery

You can use your own custom atributes to attach more information to a tag.

make a note to look into using your own tags in jquery.check out lukes hall of fame code for inspiration.

Heres a great example. I needed to mark tasks as completed or not, and using a custom attribute works well. I add the attribute “taskstatus” to be “new”, open, or “completed” based on results from a call to the database. Then I can select any task that has the attribute as so….

thetasks=$(‘#maintasklist  div[taskstatus=completed]’);

This looks for divs with my custom taskstatus set to completed, inside a container with id of maintasklist.

working with checkboxes and radio buttons

Here is a good way to get the info from checkboxes. make sure to give them all the same name, just different values. Then wrap the checkboxes in a div container with an id.In this case #c_b is a div wrapped around the checkboxes and #t is the id of a text box im using to display the results.

function updateTextArea() {
var allVals = [];
$(‘#c_b :checked’).each(function() {
allVals.push($(this).val());
});
$(‘#t’).val(allVals);
}
$(function() {
$(‘#c_b input’).click(updateTextArea);
updateTextArea();
});

 

After you have these values, of what boxes are checked, we can save the state of the checked boxes using cookies, so that the user will not have to check the boxes again when they use them…

[code]


$(‘#t’).val(allVals);
setCookie(“responsible_parties_choice”,allVals,”30″);

[/code]

To read the cookie and set the checkboxes use the code…
[code]
// look for checked box choices in cookie, then re check them
var thecheckedchoices = getCookie(“responsible_parties_choice”).split(‘,’);

for (choice in thecheckedchoices) {
//console.log(“im in there with the cookie data of ..” + thecheckedchoices[choice]);
$(“input:checkbox[value=” + thecheckedchoices[choice] + “]”).prop(‘checked’, true);
}
[/code]

 

Radio Buttons can be extremely tricky. First thing to remember is to group them using a “name” attribute.

Then they can be selected and manipulated as a group.

$("input[type='radio'][name='commencementattendanceoption']").on("change", function(){
alert($( this ).val())
});

Now I need to validate that one of them has been chosen.This will require some serious stuff. I need an include to validate things, to work with my content display system.

 

A good way to get  a comma-separated list of checkbox IDs:

$( ":checkbox" )
.map(function() {
return this.id;
})
.get()
.join();
The result of this call is the string, "two,four,six,eight".