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

Syntax question 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
Hi!

I have this javascript code inside a PHP page:


var jump_name='';
for(var i=0; i < window.parent.group_array.length; i++){
jump_name = window.parent.group_array;
document.write(\"<div id=\" +window.parent.group_array+ \" ondblclick='jump_to(jump_name)'> </div>\");
}

The problem is with the ondblclick call...the value in the called function "jump_to" always ends up being the last value in the array. Instead, I need the value written to be each value in the array as it's looped through.. Any help would be great!



 
The loop looks correct but I see you are using document.write which is generally only used when the page is first being created. And your function cannot be executing as the page loads because it is called from a dblclick event.
I believe that if you use document.write AFTER the page has loaded then you are overwriting the document rather than adding to it. You should use DOM methods to add your content to the page.



At my age I still learn something new every day, but I forget two others.
 
Dan, I think you're correct... but since the entire thing is inside a PHP page where the html portion is being echoed out, the double quotes in javascript become single or they become \".... So trying to do the translation is quite confusing. I have this close to working:

ondblclick='jump_to(\"+jump_name+\")'

only problem is when alerting the variable jump_name from within the jump_to function, I get [object] insted of the actual variable. Any more thoughts??
 
I'll just add, if you use the event handler

ondblclick

that is supported only by IE, so if you want this script to work cross-browser, you may want to just use onclick.



[monkey][snake] <.
 
Good point... here's a bit more info. Just to test syntax, I changed the code to look like this:

ondblclick='jump_to(\"+'window.parent.group_array[0]'+\")'

This works because the browser knows what's in the first position of the array. But I need it to be dynamic... When I substitute the 0 for i defined by the FOR loop, it doesn't recognize i. Not sure why... maybe I need to stick the variable i to the parent window too??
 
but since the entire thing is inside a PHP page where the html portion is being echoed out, the double quotes in javascript become single

Well - I've fixed your JS - you can escape your PHP however you like.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top