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!

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.