QUOTE(karinne @ Dec 1 2004, 02:07 PM)
hmmm... if he's using a <input type="file"...> he can't change the "browse" button
[right][snapback]19237[/snapback][/right]
True, but there again using css and javascript you can change the visibility of the file input and lay another custom button over top and transfer the value of the file input to your custom button... Of course, those with javascript disabled will see the default button, not the custom button...
Here is an example of a custom select...
Please note this example uses a button to switch between the default option menu which those with javascript disabled will see and the custom menu those with javascript enabled will see (
in a practical application the changing from default to custom would be fired when the page loads using either the window or body elements)...
CODE
<html>
<head>
<title>ListBox Upgrade</title>
<script>
function upgradeSelect()
{ scs=document.getElementsByTagName('select');
for(i=0; i<scs.length; i++)
if(parseInt(scs[i].size)>1)
new KLSelect(scs[i]);
}
function KLSelect(sc)
{ kls=document.createElement('div');
kls.className = sc.className;
for(i=0; i<sc.options.length; i++)
{ with(popt=kls.appendChild(document.createElement('p')))
{ appendChild(document.createTextNode(sc.options[i].firstChild.nodeValue));
if(sc.options[i].selected) className = 'KLOptionSelected';
else className = 'KLOption';
// title = firstChild.nodeValue;
attachEvent('onclick',KLOptionClicked);
attachEvent('onmouseover',KLOptionMouseOver);
attachEvent('onmouseout',KLOptionMouseOut);
}
popt.optionValue = sc.options[i].value;
}
input = document.createElement('input');
input.type = 'hidden';
input.name = sc.name;
input.value = sc.value;
kls.hidinp = input;
if(sc.form) sc.form.appendChild(input);
else sc.parentNode.appendChild(input);
sc.parentNode.replaceChild(kls,sc);
return;
}
function KLOptionClicked(e)
{ KLSelectOption(e.srcElement);
return;
}
function KLOptionPopUpClicked(e)
{ KLSelectOption(e.srcElement.optionNode);
e.srcElement.className = 'KLOptionPopUpSelected';
return;
}
function KLSelectOption(klopt)
{ with(klopt.parentNode)
for(i=0; i<childNodes.length; i++)
childNodes[i].className = 'KLOption';
klopt.parentNode.hidinp.value = klopt.optionValue;
klopt.className = 'KLOptionSelected';
return;
}
function KLOptionMouseOver(e)
{ if(e.srcElement.offsetWidth > e.srcElement.parentNode.offsetWidth)
{ e.srcElement.textPopUp = document.createElement('p');
e.srcElement.textPopUp.className = (e.srcElement.className == 'KLOptionSelected') ? 'KLOptionPopUpSelected' : 'KLOptionPopUp';
e.srcElement.textPopUp.style.left = getClientX(e) + 'px';
e.srcElement.textPopUp.style.top = getClientY(e) + 'px';
e.srcElement.textPopUp.attachEvent('onclick',KLOptionPopUpClicked);
e.srcElement.textPopUp.attachEvent('onmouseout',KLOptionPopUpMouseOut);
e.srcElement.textPopUp.appendChild(document.createTextNode(e.srcElement.firstChild.nodeValue));
e.srcElement.textPopUp.optionNode = e.srcElement;
document.body.appendChild(e.srcElement.textPopUp);
}
}
function KLOptionMouseOut(e)
{ if(e.srcElement.textPopUp)
if(e.toElement != e.srcElement.textPopUp)
document.body.removeChild(e.srcElement.textPopUp);
}
function KLOptionPopUpMouseOut(e)
{ document.body.removeChild(e.srcElement);
}
function getClientX(e)
{ return e.clientX - e.offsetX - document.body.clientLeft + document.body.scrollLeft;
}
function getClientY(e)
{ return e.clientY - e.offsetY - document.body.clientTop + document.body.scrollTop;
}
function testValue()
{
theform = document.getElementById('form');
for(i=0; i< theform.elements.length; i++)
{ if( theform.elements[i].name == 'test') alert(theform.elements[i].value);
}
}
</script>
<style>
.KLSelect
{ width: 125px;
height: 50px;
background: #eeeeee;
border: groove #808080 3px;
font-family: Tahoma, sans-serif;
font-size: 11px;
overflow: auto;
overflow-x: hidden;
}
.KLOption, .KLOptionSelected
{ margin: 0px;
padding: 1px 3px 1px 3px;
width: 100%;
white-space: nowrap;
cursor: pointer;
cursor: hand;
}
.KLOptionPopUp, .KLOptionPopUpSelected
{ display: block;
position: absolute;
margin: 0px;
padding: 0px 2px 0px 2px;
border: solid black 1px;
background: #eeeeee;
white-space: nowrap;
font-family: Tahoma, sans-serif;
font-size: 11px;
cursor: pointer;
cursor: hand;
}
.KLOptionSelected, .KLOptionPopUpSelected
{ background: #303030;
color: #ffffff;
}
</style>
</head>
<body>
<p>Example of emulating select control with division element</p>
<form id="form">
<select size="4" name="test" class="KLSelect">
<option value="1" selected>option 1 which is rather long</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 which is pretty darn long as well</option>
<option value="6">option 6</option>
</select>
<p>
<button onclick="upgradeSelect()"> Replace Select Control</button>
</p>
<p>
<button onclick="testValue()"> Check Value</button>
</p>
</form>
</body>
</html>
.....Willy