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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getElementById()

Status
Not open for further replies.

rachelason

Programmer
Jun 28, 2004
114
GB
Hi! i am trying to get a value of a textbox from a javascript file and then display it. It's a simple program i am testing because i have a bigger problem.
my code is

<html>
<head>
<title>index</title>
<script language="javascript" src="c.js" type="text/javascript">

</script>
</head>

<body>
<form name="myform" >
<input type="text" name="mytext" size="20" >

<input type="button" value="Show" onClick="javascript:sayHello('mytext');">

</form>


</body>
</html>

and the javascript file is

function sayHello(text1){
var temp1=document.getElementById(text1).value;
document.write(temp1);
}

I can't make the javascript file to pickup the value from the textbox....
Can someone correct my mistake? Thanks
 
You don't need the button to have the javascript: string in it (this is used in hrefs).

Here is a the input button as it should read:

Code:
<input type="button"  value="Show" onClick="sayHello('mytext');">

Have a good one,
Jeff
 
hi,i removed the javascript from the onclick.. but still it doesn't pickup the value of the textbox..any other suggestion...
 

Also, you are using "getElementById", but your text input box has no ID. Correct this by using this code:

Code:
<input type="text" name="mytext" id="mytext" size="20">

Hope this helps,
Dan
 
hi you are very helpful, but i am sorry to say i tried that aswell.... with the id and without that. Can you make the same code work in your browser.. The problem looks so simple and not able to solve it is very tiring.
 

>> Can you make the same code work in your browser

Yes. So I'm guessing you've either not told us everything, or haven't tried both fixes as suggested.

Here's what I have.. Your original code, plus Jeff's fix, plus my fix:

Code:
<html>
<head>
	<title>index</title>
	<script language="javascript" src="c.js" type="text/javascript"></script>
</head>
<body>
	<form name="myform">
		<input type="text" name="mytext" id="mytext" size="20">
		<input type="button"  value="Show" onClick="sayHello('mytext');">
	</form>
</body>
</html>

And the contents of "c.js":

Code:
function sayHello(text1) {
	var temp1 = document.getElementById(text1).value;
	document.write(temp1);
}

It all works fine for me.

Are you sure there's no code you haven't posted? For example, do you have "<script>" tags inside your "c.js" file? Did you definately try the code you posted with both our fixes?

Hope this helps,
Dan


 
Just to confirm... the following code (try copy-pasting it) works perfectly fine in my IE 6 for Windows.

Code:
<html>
<head>
<title>index</title>
<script language="javascript" type="text/javascript">
function sayHello(text1)
{
 var temp1 = document.getElementById(text1).value;
 alert(temp1);
}
</script>
</head>
<body>
<form name="myform">
<input type="text" name="mytext" id="myText" size="20">
<input type="button"  value="Show" onClick="sayHello('mytext');">
</form>
</body>
</html>

I alert the value to prove that it is being assigned to the variable.

Jeff
 
hi ! i might have some browser issue i guess, when i run the code, the browser takes a long time and comeback with an empty page(infact after a long time)... guess i should try running anti-virus.
I have no code apart from what i posted , so it should be some other virus issue. Hope so,, otherwise i wouldn't knwo what the problem is.
Thanks a lot for your help.
 
Rachel,

Did you ever get the answer as to why this didn't work in Netscape? I can use getElementByID in IE, but the code won't work in Netscape and only displays a blank page. Did you ever figure this out?

Maria
 
Case-sensitivity... id is 'myText', function is called with 'mytext'.
 
it could also give problems if you use

getElementById(xx)

instead of

getElementById('xx')

...that is if you refer directly to the id
 
Ok, ok, but I thought there may have been a browser issue, a way of using the getElementById in Netscape because...I can get the following to work in IE but it won't work in Netscape 6 or 7.

The 'my_sess_id' is the id for a hidden form field. Is this the problem? Can it not access hidden fields? Or what?

function get_sess_id()
{
var sess = document.getElementById('my_sess_id').value;
return sess;
}
 
Why couldn't you just use the following?

[tt]
function get_sess_id() {
var sess = document.form_name.my_sess_id.value;
return sess;
}
[/tt]

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
I did try that. But that doesn't wok either. And I tried the getElementByTag also and using the forms[0] array. All these work in IE but not Netscape. It's driving me mad!
 
Is there a URL associated with this webpage?

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Not yet, but the only thing I can think of is that it's something to do with the javascript being accessed as src .js file? The javascript is not therefore on source view and maybe in Netscape it needs to be? Clutching at straws here...
Thanks for trying to help.

 
Maybe you could post the following:

- Line of code that refences the .js file
- Any lines of code that call the functions in the .js file
- The entire .js file

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
*references*

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
These are the references to the .js in the html:

<script language="JavaScript1.2" src="MenuRollovers_toppro.js"></script>
<script language="JavaScript1.2" src="Browser_Check.js"></script>


The line that calls the function get_link()

link_me = get_link(id1, id2);

the variable 'link_me' is used in the following (long) line. id1 and id2 are variables from a loop (these are always ok) - you don't have to read this next line.

document.write("<tr><td><div id=\"submenu"+id1+"_"+id2+"\" onClick=\"link('"+eval("window.submenu"+id1+"_"+id2+"_link")+"','"+submenu_url_target+"');\" onMouseOver=\"over_submenu["+id1+"]=true; setItemColor('submenu"+id1+"_"+id2+"','"+submenu_highlight_color+"','"+submenu_font_highlight_color+"');\" onMouseOut=\"over_submenu["+id1+"]=false; deactivateAllDelay(); setItemColor('submenu"+id1+"_"+id2+"','"+submenu_background_color+"','"+submenu_font_color+"');\" style=\"position:relative; width:"+submenu_width+"px; height: "+submenu_item_height+"; z-index:10; left: 0px; top: 0px; visibility: inherit; background-color: "+submenu_background_color+"; layer-background-color: "+submenu_background_color+"; color: "+submenu_font_color+"\"><a id=\"submenu"+id1+"_"+id2+"a\" href='"+link_me+"' target='"+submenu_url_target+"' style=\"text-decoration:none; color: "+submenu_font_color+";\"><img src=\"spacer.gif\" width=\""+submenu_item_indent+"\" height=\"1\" border=\"0\"><font face=\""+submenu_font_face+"\" size=\""+submenu_font_size+"\">"+eval("window.submenu"+id1+"_"+id2+"_text")+"</font></a></div></td></tr>");

And finally the functions:

function get_link(id1, id2)
{
sess_id = get_sess_id();
return eval("window.submenu"+id1+"_"+id2+"_link") + sess_id;
}

function get_sess_id()
{
var sess = document.getElementById('my_sess_id');
return sess;
}
 
Your get_sess_id() function is returning the element named my_sess_id, not the value of my_sess_id.

Is this what you want?

*cLFlaVA
----------------------------
A pirate walks into a bar with a huge ship's steering wheel down his pants.
The bartender asks, "Are you aware that you have a steering wheel down your pants?"
The pirate replies, "Arrrrr! It's driving me nuts!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top