Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get value of textbox/input-element 1

Status
Not open for further replies.

mysqlfrk

Programmer
Aug 3, 2011
12
Hi,
I want to create an input element and a button,and ask user to give a color name in the input box,and after clicking the button the color of the div will change.

Now here is my code . I tried this code but it doesnot work-
<html>
<head>
<script type='text/javascript'>
function fun() {
var cr=document.getElementById('nm').value;
var element = parsedocument.getElementById("example");
element.style.backgroundColor=cr;
}
</script>
</head>
<body>
<div id="example" onClick="fun()">
<br><br><center><b>Click the Submit button to change the color of this div
as per input</b></center></div>
<br> <br>

Enter Color: <input type="text" id="nm"><br><br><br>
<input type="submit" value="Submit">
</body>
</html>
What can be wrong with the above code?
Hope you can help me.
Thanks in advance.
 
Hi

There is an extra "parse" word in your code. Remove it and will work. [sup](*)[/sup]

[small](*) Though probably not as intended. The [tt]submit[/tt] button is ( fortunately ) useless there, you have to click the [tt]div[/tt] to get the color change.[/small]


Feherke.
 
Hi
Thanks but firstly the parse word was by mistake. I was going to type a "parseInt". Anyways I removed it and yes when I click on the div it does change.
But that is what is not intended. As you can see @feherke, the objective is to "ask user to give a color name in the input box,and after clicking the button the color of the div will change."
So can you please tell me how to do that?
 
Hi

Currently the event handler is set in [tt]div[/tt]'s [tt]onclick[/tt] attribute, so the color change works when the [tt]div[/tt] is clicked.

Now where you should move the [tt]onclick[/tt] attribute if you want the event handler to be triggered on button click instead of [tt]div[/tt] click ?


Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top