PDA

View Full Version : Passing field name through javascript?



martynball
Jan 18th, 2010, 02:17 PM
I want to be able to use on function to check if a field has a value typed in on keyup.

But I do not understand how I can get the javascript to pull the value in from the HTML. Here is my JavaScript:



<script type="text/javascript">
function validate_now(name) {
if (document.upload.(name).value.length == 3) {
alert("True");
return true;
else {
document.upload.(name).style.backgroundColor = "#990000";
return false;
}
}
</script>


HTML:


<input type="text" name="imageTitle" class="inputField"
onkeydown="limitText(this.form.imageTitle,this.form.countdown1,30);"
onkeyup="limitText(this.form.imageTitle,this.form.countdown1,30); validate_now('imageTitle')" />
<input readonly type="text" name="countdown1" size="3" value="30" style="background-color:#999999; border:0px; color:#339900; font-family:Calibri; font-weight:bold;">

abduraooft
Jan 18th, 2010, 02:24 PM
validate_now(this)

function validate_now(obj) {
if (obj.value.length == 3) {
alert("True");
return true;
else {
obj.style.backgroundColor = "#990000";
return false;
}
}

martynball
Jan 18th, 2010, 02:57 PM
Still not working :(

abduraooft
Jan 18th, 2010, 03:27 PM
Still not working :(
Actually, what's your ultimate aim? :)

onkeydown="limitText(this.form.imageTitle,this.form.countdown1,30);" and what's the purpose of the above code?

martynball
Jan 18th, 2010, 06:51 PM
The function of the first code is to display the character limit, which counts down.

My end goal is to make the field background/border a red colour whilst the minimum number of characters is not input. Then turn green when minimum number of characters is met.

Old Pedant
Jan 18th, 2010, 08:33 PM
And what happens if somebody uses ONLY the mouse to copy/paste code into the field???

And do you have a max size, as well??

I prefer to do this via a timer. Every quarter second, or so, I go out and check the length and then show the min/max (and any effects you want) results.

rnd me
Jan 18th, 2010, 10:40 PM
And what happens if somebody uses ONLY the mouse to copy/paste code into the field???


there is the little used onpaste event...