javascript problem - probably very simple

Associate
Joined
24 Jul 2004
Posts
1,580
Location
Preston, Lancs
ok, so i have a pretty decent understanding of php but am in need of some jquery based elements in my script (an image cropper).

so, in php I would do something like this:

PHP:
for($x=0;$x<10;$x++)
{
       echo '<input type="hidden" name="ft_'.$x.'" id="ft_'.$x.'" value="1">';
       echo '<input type="hidden" name="fl_'.$x.'" id="fl_'.$x.'" value="1">';
}

The equivalent javascript - in my head - would be this:

Code:
for($x=0;$x<10;$x++)
{
                    Dom.setAttribute('ft_'+ x.toString(), 'value', region.top);
                    Dom.setAttribute('fl_'+ x.toString(), 'value', region.left);
}

but this doesn't work...

I'm struggling to find good javascript resources and dont really have time to wait for an amazon book. Tizag / w3schools are my php helpers of choice, but the Javascript stuff isn't brilliant.

Any pointers would be most appreciated. :confused:
 
Soldato
Joined
9 May 2005
Posts
4,524
Location
Nottingham
This can probably be done really easily using jQuery, are you asking how to append new elements to the DOM using javascript in a loop? Might be worth providing a more concrete example, what sort of content are you trying to put into the DOM?
 
Associate
OP
Joined
24 Jul 2004
Posts
1,580
Location
Preston, Lancs
aye - i guess this is one of those probs where it's hard to explain but seems like it should be basic in my head.

Found a horribly hacky way of doing it:

Code:
                    Dom.get('t_<?php echo $x; ?>').innerHTML = region.top;
                    Dom.get('l_<?php echo $x; ?>').innerHTML = region.left;
                    Dom.get('h_<?php echo $x; ?>').innerHTML = region.height;
                    Dom.get('w_<?php echo $x; ?>').innerHTML = region.width;

not there yet, but closer.
 
Associate
Joined
25 Jan 2009
Posts
1,342
Location
London
http://jsfiddle.net/ZkrFE/1/

something like this?
Can also do it in core JavaScript pretty easily

bah, nevermind, didn't read the entire thread entirely and misunderstood.. lol

edit:
So from my understanding, you want hidden input elements created within a loop and assigned to be assigned different values depending on whatever region's current properties are..
 
Last edited:
Associate
OP
Joined
24 Jul 2004
Posts
1,580
Location
Preston, Lancs
cheers - and thanks for the examples, very helpful - much easier to understand when you can see the code and outcome.

I have this code almost finished actually, the strange thing is - it works fine when statically coded, but when php generates the exact same code, it doesnt work as intended.

Bugs, gotta love em.
 
Back
Top Bottom