Jump to content

[SOLVED] how to set new value to input box, via function?


Jarod

Recommended Posts

Okay so I am wanting to change my input field from Username to WhatEverHere, this seems to not be working for me though, here is my html code:

<input type="text" class="sideBarInput_Login" id="userinp" name="user" value="Username" onclick="setInputFieldDefault(' ',userinp);" />

As you see I have declared setInputFieldDefault() in the html code, it's not working at all though (at least I think).

 

 

and here is the external javascript code:

function setInputFieldDefault(newValue,inputId) {
var inputDefaultValue = document.getElementById(inputId).value;
if(inputDefaultValue == inputDefaultValue) {
	inputDefaultValue = newValue;
}
}

What the first parameter does is it sets a new value to the assigned ID element, in this case though it sets a new value for the <input> with the ID userinp, the last parameter is the ID of that element; in which the first parameter is assigned to change. I can't change the assigned elements value though -.-... Help please!

A few things:

 

1. In your input element, you don't actually supply the new value to the function.  Instead, you're passing an empty string (' ').

 

2. Since the id is a string, you need to put it in quotes (i.e., setInputFieldDefault('WhatEverHere', 'userinp'); ).

 

3. The if-conditional in your function makes no sense.  You're checking to see if the default value equals itself, which will always be true.  Instead, you need:

 

function setInputFieldDefault(newValue, inputId) {
   var inputField = document.getElementById(inputId);

   if(inputField.value == newValue) { return; } //old value and new value are the same - ignore
   else { inputField.value = newValue; } //they're different, so assign the new value to the input
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.