Jarod Posted November 2, 2009 Share Posted November 2, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/179892-solved-how-to-set-new-value-to-input-box-via-function/ Share on other sites More sharing options...
KevinM1 Posted November 2, 2009 Share Posted November 2, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/179892-solved-how-to-set-new-value-to-input-box-via-function/#findComment-948995 Share on other sites More sharing options...
Jarod Posted November 2, 2009 Author Share Posted November 2, 2009 the code isn't working still :-\, look at this: website http://jay-test.freehostia.com/index.php javascript file http://jay-test.freehostia.com/js/basicFunctions.js Quote Link to comment https://forums.phpfreaks.com/topic/179892-solved-how-to-set-new-value-to-input-box-via-function/#findComment-949020 Share on other sites More sharing options...
KevinM1 Posted November 2, 2009 Share Posted November 2, 2009 That's because you have a typo. Remember: your funcction is setInputFieldDefault(newValue, inputId) - you need to put the arguments into your function in the correct order. There's no element on your page with an id of Username. Quote Link to comment https://forums.phpfreaks.com/topic/179892-solved-how-to-set-new-value-to-input-box-via-function/#findComment-949026 Share on other sites More sharing options...
Jarod Posted November 2, 2009 Author Share Posted November 2, 2009 Thanks :o@! Quote Link to comment https://forums.phpfreaks.com/topic/179892-solved-how-to-set-new-value-to-input-box-via-function/#findComment-949028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.