/    Sign up×
Community /Pin to ProfileBookmark

getting value of a variable

Hi guys,

I have a DHTML menu, and on the menu there is a functionality that one child is right-clicked, a user is given a choice to add this child (a program description) to a database. This is the code:

[code=php]
// menu item
<li><a href=”blah.jsp” title=”Program1″><span name=”Program1″ oncontextmenu=”javascript:addToFavorites(this);return false;”>Program1</span></a></li>

//addToFavorites
var favoriteObject;
function addToFavorites(obj){
favoriteObject = obj.name;
showHide(‘addToFavoritesLayer’,’visible’);
showHide(‘KWILogoLayer’,’hidden’);
}
[/code]

Now, in the layer addToFavoritesLayer, I have a document.write for favoriteObject:

[code=php]
<td align=”center” valign=”middle” colspan=”2″ class=”favoritesMsg”>Add <script>document.write(favoriteObject);</script> to Favorites?</td>
[/code]

The problem is that after addToFavorites() is called, and addToFavoritesLayer is made visible, document.write(favoriteObject) is coming in as undefined. In layer I have a link that calls getObject() function:

[code=php]
function getObject(){
showHide(‘addToFavoritesLayer’,’hidden’);
showHide(‘KWILogoLayer’,’visible’);
addFavItem(favoriteObject);
}

function addFavItem(item){
document.getElementById(‘favList’).src=”favorites/addToFavorites.jsp?item=” + encodeURL(item);
showHide(‘Favorites’,’visible’);
}
[/code]

and that works fine, so how come with document.write() I have a problem?

thanks,
camokat

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 20.2005 — because it writes a new document, effectively you have no function, no content
Copy linkTweet thisAlerts:
@camokatauthorJun 20.2005 — thanks, so what's the solution?
Copy linkTweet thisAlerts:
@camokatauthorJun 20.2005 — Ok got it:
[code=php]
var favoriteObject;
function addToFavorites(obj){
favoriteObject = obj.name;
alert(favoriteObject);
document.getElementById('favProgDescr').innerHTML = favoriteObject;
showHide('addToFavoritesLayer','visible');
showHide('KWILogoLayer','hidden');
}
[/code]
×

Success!

Help @camokat spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...