Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Acessing Function Properties

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I access a property within a function from the global scope? I need to create a copy of
document.getElementById(''+id+'') so that I can carry out a function with two DIFFERENT id arguments.here is my code:
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var a = 0;
var b = the other copy, but how do i reference id from outside of the function it exists in?
function ToggleSource(id,c){
if(a == 0){document.getElementById(''+id+'').src = "images/" + c; a++};
else if(a == 1){document.getElementById(''+id+'').src = b; a++};
else{a = 0; document.getElementById(''+id+'').src = "images/" + c; a++};}
// End -->
</script>
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
I may be misinterpreting your question, but could you do this?

If not, maybe you could be more specific about what you'd like to do?
g.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this
function ToggleSource(id,c){
var id = id;
if(a == 0){document.getElementById(''+id+'').src = "images/" + c; a++};
 
Michael McNally
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am finding the below code a success in this Toggle function. The only way i could get it to work was by turning the default left and top positions into arguments. Otherwise when I tried to store document.getElementById(''+id+'') within a variable...when the value of any property of the id was changed...so was the property within the stored variable. Below, im trying to toggle absolute positions of an image.

var a = 0;
function TogglePosition(id,defaultLeft,defaultTop,Left,Top){
if(a == 0){document.getElementById(''+id+'').style.posLeft = Left; document.getElementById(''+id+'').style.posTop = Top; a++};
else if(a == 1){document.getElementById(''+id+'').style.posLeft = defaultLeft; document.getElementById(''+id+'').style.posTop = defaultTop; a++};
else{a = 0; document.getElementById(''+id+'').style.posLeft =Left; document.getElementById(''+id+'').style.posTop = Top; a++};}
-------------------------------------
The code within the body section goes like this...
<img id="myToggleableImage" oncliiick="TogglePosition('002menu_bar','0','16','64','64');" style="position:absolute; left:16; top:16;">
-----
The above oncliiick is spelt incorrectly for this forum alone
When the image is clicked...the absolute position goes from left:0; top:16; to.. left:64; top:64;
-----------------------------------
What i really want is simply three arguments.
(id,Left,Top)
Instead of using DefaultLeft and DefaultTop arguments...I am trying to store the default absolute positioning within two variables. So i tried to store them in these variables within the function:
var defaultLeft = document.getElementById(''+id+'').style.posLeft;
var defaultTop = document.getElementById(''+id+'').style.posTop;
That didnt work Any ideas?
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic