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!

what is the diference?

Status
Not open for further replies.

julk

Programmer
Jun 23, 2004
10
US
hi i am tring to make a n object get the focus like this

RitchText.focus();

but for some reason it does not work with

document.getElementById('RitchText_suc1').focus();
the defenition for the object is like this
<iframe class='RitchText' height='99px'
width='593px' ID="RitchText" >
</iframe>
what is the diference between refrencing an object directly by its name, nad by the "getElementById" method in the document object?
the problem is that i cant use the direct name because i wont know the name (id) of the <IFRAME>. so dinamically(getElementById) is the only way to go for me.

thank you in advance.



 
You are trying to set focus on RitchText_suc1
>document.getElementById('RitchText_suc1').focus();

while the definition of the object is RitchText
>ID="RitchText"
 
ups im sorry for that
the actual code does have the maching names
any ideas?.
 
When you getElementById on the IFRAME, you get the actual tag, not the contents. I have tried, but I don't see how to go from the tag to what's inside.

What you CAN do (at the risk of a probably-minor-to-imperceptable slow-down) is:

Code:
eval('RitchText').focus();

See if that works for you.

--Dave
 
great it worked!!
:> thanks alot LookingForInfo
 
Don't use eval() if you can help it.

document.frames.RitchText.focus()
Or
document.frames.RitchText.document.getElementById("myFieldInIframe").focus()

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Adam,

I understand about avoiding 'eval', but julk said the iframe id had to be dynamic (wouldn't be known at the time of coding) and we couldn't figure out another way to send focus INSIDE the iframe otherwise.

In the above lines you proposed, from what I understand, 'RitchText' would have to be a variable.

getElementById wasn't working for us. Other ideas besides eval?

--Dave
 
document.frames['RitchText'].focus();
Or
document.frames['RitchText'].document.getElementById("myFieldInIframe").focus();

where 'RitchText' could be replaced with a variable.

See faq216-3858 for more info

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
!!!

Must be why I couldn't see it... it was right in front of me!

I should know by now that if I am thinking of using eval, then there's a better way!

Thanks, Adam.

--Dave
 
ill try it adam thanks for the tip.
this forum rocks!
 
From the incredibly long-to-open page:
link said:
In JavaScript, eval( ) is a top-level, built-in function that converts any string to a block of code and then executes that block of code. The syntax for JavaScript's eval( ) is:

eval(string)
When eval( ) is executed in JavaScript, the interpreter converts string to code, runs that code, and returns the resulting value (if a value is generated). Consider the following JavaScript examples:

eval("parseInt('1.5')"); // Calls the parseInt( ) function, which returns 1
eval("var x = 5"); // Creates a new variable named x and
// sets its value to 5
If you've never seen eval( ) before, you may be thinking, "When would I ever have a string with code in it? Why not just write the code out?" Because eval( ) lets you dynamically generate code when you need to. For example, suppose you have ten functions named sequentially: func1, func2, func3, ..., func10. You could execute those functions with 10 function-call statements:

func1( );
func2( );
func3( );
// etc...
But you could also execute them more conveniently using eval( ) in a loop, like this:

for (i = 1; i <= 10; i++){
eval("func" + i + "( )");
}

...and...

ibid said:
Note, however, that eval( ) can be quite processor-intensive. In more demanding scenarios, we're better off using the array-access...

I know this to be true, but only use eval() sparingly anyway, so I haven't really seen the slowdown that people associate with this "process hog."

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top