/    Sign up×
Community /Pin to ProfileBookmark

syntax question (simple i guess)

i’ve got a -maybe dumb- question since i’ve never had a single js lesson ?

i want to catch some divs in a page, at first i got all the names of them from a db, but now they are dynamicly generated.
so.. i couldnt use php anymore.. so.. this is what i came up with:

[CODE] if (obj.name.match(‘resize’))
{
instance = str.replace(/resize/,””);

elements.q+instance+.resizeTo(dd.obj.x-elements.q+instance+.x+12, ((obj.y+12)-dd.elements.q+instance+.y));
elements.opt+instance+.moveTo(dd.obj.x-24, dd.obj.y);
elements.movemove+instance+.moveTo(dd.obj.x-12, dd.obj.y);
}[/CODE]

well, this doesnt work.
example:
object name is “resize[B]1[/B]
i strip resize
and i want to get:
elements.movemove[B]1[/B].moveTo(dd.obj.x-12, dd.obj.y);

you know what i mean ?

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@konithomimoNov 11.2006 — Well, you can do what you have or use split, but it depends on what you are trying to do. For example, what is str? Is it the string located below, is it an input string, or is it from a DB?
Copy linkTweet thisAlerts:
@jonavanmonaauthorNov 12.2006 — str. is for example resize1.

its a given variable.

so in one way or another i have to get the number ('1' in this case), glued into elements.movemove.moveTo(dd.obj.x-12, dd.obj.y); so it becomes

elements.movemove1.moveTo(dd.obj.x-12, dd.obj.y);
Copy linkTweet thisAlerts:
@konithomimoNov 12.2006 — You can use replace like you have it, or you can use split. for example, here it is with replace:
<script type="text/javascript">
function changeName(){
var divs = document.getElementsByTagName('div');
var changeFrom = 'resize';
var changeTo = '';
var hold, instance;
for(var i=0;i<divs.length;i++)
{
if(divs[i].name.match(changeFrom))
{
instance=changeFrom.replace(changeFrom,changeTo);

hold = "elements.q"+instance+".resizeTo(dd.obj.x-elements.q"+instance+".x+12, ((obj.y+12)-dd.elements.q"+instance+".y));
elements.opt"+instance+".moveTo(dd.obj.x-24, dd.obj.y);
elements.movemove"+instance+".moveTo(dd.obj.x-12, dd.obj.y)";
}
//now do whatever you want to do with the variable hold...
return;
}
</script>
Copy linkTweet thisAlerts:
@jonavanmonaauthorNov 12.2006 — thanks for your help!

you know the thing is, i dont need it as a variable(string).

i mean, in the function -elements.movemove1.moveTo(dd.obj.x-12, dd.obj.y);- moves an object (a div) and if its in a variable, it won't move anything.

right? ?
Copy linkTweet thisAlerts:
@jonavanmonaauthorNov 12.2006 — and when i do

elements.movemove+number+.moveTo(dd.obj.x-12, dd.obj.y);

i get a syntax error
Copy linkTweet thisAlerts:
@konithomimoNov 12.2006 — that is because using the addition signs makes it a string. That is why i left it as a variable. Once you have the variable you can eval(), thus treating it as regular code. That was the purpose of making it appear as a string. using the quotation marks makes it a proper string, thus no syntax eror . . . so basically, change this:

//now do whatever you want to do with the variable hold...

to this:

eval(hold);


as well, make sure to add another closing bracket since I only included two and three are needed.
Copy linkTweet thisAlerts:
@jonavanmonaauthorNov 12.2006 — thats exactly what i need, you're great?
Copy linkTweet thisAlerts:
@konithomimoNov 12.2006 — No problem. I should mention though that eval() uses a lot of system proceses, so it can run slow sometimes.
×

Success!

Help @jonavanmona 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.1,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...