Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Pennsylvania, United States
    Posts
    54
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Exclamation Update value of hidden input element on textarea onChange?

    I have an idea for a little script.....I'm just stuck on one little part: When a user changes the value in a input or textarea box, I want it to change the value of a certain hidden input tag too.

    Here's what I have so far:

    function getNewValue(inputhidden, textinput) {
    var data = document.getElementById(inputhidden);
    var text = document.getElementById(textinput);
    data.value = text.value;
    }

    <input type= "text" name= "name" id= "name" onChange="getNewValue('detail', 'name')">
    <input type= "hidden" name= "detail" id="detail" value= "">

    However, this is not working and I'm not sure why

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by johnnnn View Post
    I have an idea for a little script.....I'm just stuck on one little part: When a user changes the value in a input or textarea box, I want it to change the value of a certain hidden input tag too.

    Here's what I have so far:

    function getNewValue(inputhidden, textinput) {
    var data = document.getElementById(inputhidden);
    var text = document.getElementById(textinput);
    data.value = text.value;
    }

    <input type= "text" name= "name" id= "name" onChange="getNewValue('detail', 'name')">
    <input type= "hidden" name= "detail" id="detail" value= "">

    However, this is not working and I'm not sure why
    try to change onChange to lowercase, 'onchange'. I don't think that 'name' is a proper value of name and id attributes but if I'm not wrong this is not a problem, maybe in IE, I don't know.

    best regards

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by oesxyl View Post
    I don't think that 'name' is a proper value of name and id attributes but if I'm not wrong this is not a problem, maybe in IE, I don't know.
    In fact it is permissible (but not very wise) to give an HTML element the name "name".

    In Internet Explorer, names and IDs are global variables and thus you should NEVER use a global variable or function name which is the same as an HTML element name or ID. You should also avoid giving names or id's to your Javascript variables/functions/arguments/forms words which are JavaScript methods/properties/attributes such as 'name' or 'id' or 'value'.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Your code works. No need to modify onChange to onchange in the html (although it's good practice to use all lowercase in html). Html is not case-sensitive unlike Javascript.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    function getNewValue(inputhidden, textinput) {
    var data = document.getElementById(inputhidden);
    var text = document.getElementById(textinput);
    data.value = text.value;
    }
    </script>
    <body>
    <form>
    <input type= "text" name= "name" id= "name" onChange="getNewValue('detail', 
    'name')">
    <input type= "hidden" name= "detail" id="detail" value= "">
    </form>
    </body>
    </html>
    After loading the page and typing something in the field, quickly verify that the hidden input's value changed by typing this in the browser's address bar and hitting Enter key:

    Code:
    javascript:alert(document.getElementById('detail').value)
    It should alert the value entered in the 'name' field.

  • Users who have thanked glenngv for this post:

    oesxyl (12-06-2009)

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by glenngv View Post
    After loading the page and typing something in the field, quickly verify that the hidden input's value changed by typing this in the browser's address bar and hitting Enter key:

    Code:
    javascript:alert(document.getElementById('detail').value)
    It should alert the value entered in the 'name' field.
    I didn't know that js can be used this way, I use ffox 3 with noscript installed and when I try this ends with a xss alert. Seems normal but is some way to avoid this?
    maybe what I type was wrong,
    Code:
    .....html?javascript:alert('alert this');
    best regards


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •