[web] Using the input tag with javascript.

Started by
5 comments, last by evolutional 19 years, 7 months ago
I'm trying to make it so that when you hover over one of my links a description of it will appear in a text field. I tried to do it like this: this is in the heading:

<script language="javascript">
mystring='';
function display(){
mystring='You Suck';
}
</script>

this is on the link:

<a href = "dragonball.html"class="nav" onMouseOver="display()"><strong>&#187</strong>Dragon Ball</a>

and this is my text field:

<script language="javascript">
<input type="text" value=mystring name="input1"></input>
</script>

Keep in mind i'm not really experienced with programming &#106avascript. It's probably something in the syntax but i thought that when there is an error in &#106avascript code it says it where it would normally say done. So it must be in my html code. Help is very appreciated. Thanks in advance.
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Advertisement
There are several things wrong with this.

An input element declaration, is not &#106avascript code, and should not be put inside <script> tags.

Input elements should probably be put inside a form (even if the form is never submit).

This means if you need to change the contents of the input, you can do:

function display() {  var myInput = document.forms[0].input1;  myInput.value = "You suck";}


Which should work, and uses only the so-called "DOM0" DOM functions, which work on practically any browser that has &#106avascript support.<br><br>You will need to give the input a name.<br><br>Alternatively, you can give the input object and ID instead, and use document.getElementById() to find it. This is a "DOM1" method, but still supported by most browsers.<br><br>This method also alleviates the need to put the input inside a form.<br><br>Mark
Tahnks for the quick reply i'll mess around with what u gave me til i get it right =D
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
so would i do in the header:
<script language="&#106avascript">
function giveDes(){
var myInput = document.getElementById(1);
myInput = "You suck!";
}
then down in the body:
<input type="text" id="1" value="" onMouseOver="giveDes()"></input>
Is that the way i should do it?
Favorite Quotes:Gandalf: You cannot pass!|Smeagol: We don't need you!|Sloth: Hey you guys!|
Instead of giving the element a numeric ID, give it a string ID as it could cause problems later on for the js interpreter.


This should work...

<script language="javascript">function giveDes(){var myInput = document.getElementById("myID");myInput.value = "You suck!";}<input type="text" id="myID" value="" onMouseOver="giveDes()"></input>


What you're doing is getting a named element from the DOM, then asisgning a value to it's .value member attribute. If you're unsure, check the Forum FAQ for some links and tutorials on &#106avascript.
Quote:Original post by evolutional
Instead of giving the element a numeric ID, give it a string ID as it could cause problems later on for the js interpreter.


No it couldn't.

If you understand &#106avascript, you will realise how straightforward it is.<br><br>But I'd agree, using a name would make the code more maintable.<br><br>Mark
You can also reference elements by offset, not with the getElementById function, but when you're accessing collections/arrays and such. I'd generally steer away from numeric id names because it'd lead to confusing code in the long run.

This topic is closed to new replies.

Advertisement