Having to mess with forms all the time, I was beginning to go crazy tweeking different messages. One for success, one for failure, one for help, etc.
I decided to try to combine some css with some javascript to make a nice reusable piece to display my form messages. Here is the fruit of that labor.
The function is going to take three arguments. The first argument is a message and or list of messages. The second is the id of the node you would like to stick the message before. The last argument is a classname.
/*global document */
function showMessage(thismessage, location_id, classname) {
//first clear showmessage div
if (document.getElementById("showmessage")) {
var clearerror = document.getElementById("showmessage");
clearerror.parentNode.removeChild(clearerror);
}
The first part of the function will check to see if the div “showmessage” exists. If it does then it will remove it from the document.
// create an showmessage div
var location = document.getElementById(location_id);
We define the location so it can be used later. Below we create a new div and set the id attribute to showmessage. We then can style the div for each instance using the className. After that we create an unordered list.
With the list ready, the messages can then be put into an array. We will split the messages up in this example with a pipe “|”. You can substitute your own delimiter.
var showmessagediv = document.createElement("div");
showmessagediv.setAttribute("id", "showmessage");
showmessagediv.className = classname;
var showmessageul = document.createElement("ul");
showmessageul.setAttribute("id", classname + "list");
var showmessage = [];
showmessage = thismessage.split("|");
var i;
for (i = 0; i < showmessage.length; i++) {
var showmessageli = document.createElement("li");
var showmessagelitxt = document.createTextNode(showmessage[i]);
showmessageli.appendChild(showmessagelitxt);
showmessageul.appendChild(showmessageli);
}
showmessagediv.appendChild(showmessageul);
location.parentNode.insertBefore(showmessagediv, location);
// have to add this to display error until css corrected
}
Looping through the array, we create a new list element and insert the message. We append the message to the new list element and then append the list element to the unordered list element. When it is all done then we append the unordered list to the showmessage div. The last step of the function is to insert the new showmessage div right before the location_id.
Attached is the style I used with both the successful messages and the unsuccessfull images.
.errormsg {
clear: both;
display: block;
width: 90%;
color: #FFFFFF;
font-weight: bold;
background-color: #FF9D9D;
padding: 9px 10px 6px 40px;
margin: 10px 0;
border: 2px solid #FF0000;
}
.successmsg {
clear: both;
display: block;
width: 90%;
color: #333333;
font-weight: bold;
background-color: #9bc19b;
padding: 9px 10px 6px 40px;
margin: 10px 0;
border: 2px solid #039b03;
}
#errormsglist, #successmsglist {
margin-left: 0;
padding-left: 0;
list-style: none;
}
#errormsglist li, #successmsglist li{
padding-left: 20px;
background-repeat: no-repeat;
background-position: 0;
}
#errormsglist li { background-image: url(/icon/exclamation.png); }
#successmsglist li { background-image: url(/icon/accept.png); }
You can modify the CSS and change the class name to what ever you wish. The only think to remember is if you style the list, to append “list” to the style.







