Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-05-2009, 05:29 PM   PM User | #1
johnnnn
New Coder

 
Join Date: May 2009
Location: Pennsylvania, United States
Posts: 54
Thanks: 16
Thanked 0 Times in 0 Posts
johnnnn is an unknown quantity at this point
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
johnnnn is offline   Reply With Quote
Old 12-05-2009, 05:37 PM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Old 12-05-2009, 06:57 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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'.
Philip M is offline   Reply With Quote
Old 12-06-2009, 08:44 AM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
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.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Users who have thanked glenngv for this post:
oesxyl (12-06-2009)
Old 12-06-2009, 09:47 AM   PM User | #5
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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
oesxyl is offline   Reply With Quote
Reply

Bookmarks

Tags
hidden, input, onchange

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:44 PM.


Advertisement
Log in to turn off these ads.