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 Chriss Miller 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-field

Status
Not open for further replies.

BennySHS

Programmer
Mar 15, 2005
32
DE
Hi there,

I'm trying to read the value of a textbox and/or a hidden input field.

I tried the following code:
alert(document.getElementById('bereich').text);
and alert(document.getElementById('bereich').value);

and both doesnt work. I goolged for a while to find other solutions, but they are all the same as mine.
Is there a obvisiuos mistake i make ?

I use Firefox 1.07 but in IE it doesn't work to.

Hope you can help me,
thx in advance.
greets ben
 
The correct line is your second line.
>[tt]alert(document.getElementById('bereich').value);[/tt]
If it does not, you have mistake elsewhere and the alert has never been reached.
 
As Tsuji says, the correct alert is the second one. You may not be getting the value alerted for many reasons - including missing the id on the input. You also want to make sure you do not attempt to access the DOM until after the page has loaded (and the onload event has triggered).

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
hi guys,

thx a lot for your answers.

>You also want to make sure you do not attempt to access the >DOM until after the page has loaded (and the onload event has >triggered).

Guess that's the problem.

I tried to get the Value of the element in a if-condition at the top of the page.
Its more then logic that the DOM can't access the element right now...

Ok so I'll find another way to do this ;)
Thx a lot!
greets,
ben
 
Replace the alert with a basic alert("Got here"); and see if that fires. Then you know if it is the logic getting to that point or the actual reading of the value.

Make certain your field has an ID tag. Try:
<input type="text" id="bereich" value="Testing">

And at what point do you execute the code with the alert?
As BabyJeffy said, it has to occur after the page has been fully loaded.
Perhaps try even putting your alert in an onload statement in the body tag for testing.

Paranoid? ME?? WHO WANTS TO KNOW????
 
i have two suggestions. you could either

1) place the code at the bottom of your page:

Code:
<html>
  <head>
    <title>stuff</title>
  </head>
  <body>
    ...

  <script type="text/javascript"><!--
  alert(...);
  //--></script>
  </body>
</html>

or 2) place the code in a function and call it from the body's onload event.


Code:
<html>
  <head>
    <title>stuff</title>

<script type="text/javascript"><!--
function doSomething() {    
    alert(...);
}
//--></script>
  </head>
  <body onload="doSomething();">
    ...

*cLFlaVA
----------------------------
[tt]your mom goes to college[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
You can also add the parameter defer]/b] to your script tag. That will keep it from running until the page has been loaded.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Tracy,

I hadn't heard of defer before, but upon looking it up, I see it might not have cross-browser support (introduced for IE originally) and, it appears, that great care must be used in using it. More information at
I'm curious if there is a benefit to using defer versus the BODY tag's ONLOAD event.

Opera seems to have something called DOMContentLoaded ( which operates like defer. I'm not sure what other browsers do.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
is the id correct? i mean is is spelt correctly and all?
you could try Elements["nameofyourelement"].value right?
 
From a few posts back...
Guess that's the problem.

I tried to get the Value of the element in a if-condition at the top of the page.

I suggested that he not attempt to access the DOM until the onload event has triggered. His reply indicates he was running some JS in the page as it was loading.

So... unless it turns out to be something else, I think we've cracked this one [smile]

Cheers,
Jeff

[tt]Jeff's Page [/tt][tt]@[/tt][tt] Code Couch
[/tt]
 
Hi there,

thx to all for the much replies and tips.

I'll explain short what I wanted to achive:

On my pages are two forms. Every form has its own submit-button.
Now when submitting form b I wanted to access via JS the content of form A. And the access to the content of form A happened during the pages was not finished loading.

I'll do a workaround to set a hidden-field in form B with the content of form A with an onclick, and then I can access the fields of form B via php.

Sorry guys I guess I should had explained my intention a littlebit earlier, so I would have saved you some writing..

Thx a lot again,
greets ben

ps: sorry for the bad grammar
 
Benny,

If I understand you correctly, then you can just keep the button that submits form B disabled until the forms finish loading.

Code:
<body [red]onload='document.forms["formB"].submitButton.disabled = false;'[/red] />
...

<input name='submitButton' type='Submit' [red]disabled[/red] />
...

'hope this helps.

--Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top