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

newbie help with getelementbyid

Status
Not open for further replies.

Joshhhhh

Programmer
Jan 4, 2007
5
0
0
I have the following function:
<script language="JavaScript" type = "text/javascript">
function returnObjById( id )
{
var returnVar = document.getElementById(id);
return returnVar.value;
}
</script>

How can I transmit the value of the function to the value of the textfield

<input type="text" value="???"/>
 
You are not providing sufficient information to give you an exact answer but here it goes:

The syntax you are using works if the function returnObjByID() is called by a 2nd function which in turn will use the returned value to inject it in the field object. If that is the case, there might be a degree of redundancy.

That being said, you need to have a name or an ID assigned to the input object field for you to reference it through your JS. So, if you wanted to inject value to a field you will then use:
Code:
document.getDocumentById(id).value='some value';

You could also use
Code:
document.form_name.field_name.value='some value';

As you can see, you need a name or an ID.

Now, to best answer your question you will need to be specific as to how you intend to use the above function.

Hope this helps!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
How can I transmit the value of the function to the value of the textfield

Something like:

Code:
yourObjRef.value = returnObjById(someId);

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
@feherke,

Or not, if you refer it by order number. For example the first form's first field :

document.forms[0].elements[0].value='some value'

What would happen if new objects are added above this one? It seems that the code will break since what was once element 0 may shift and become an entirely different one.

I can see where this could be useful but cannot see how in this case if OP is looking to hit a specific object; which no matter where in the form it is found it must be unique.

That said, I get your point of that name and/or ID are not exclusive methods to reference an object.



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top