strange to say, i dont see my reply. if this reply is shown twice i
appologize.

i have to show you my modification, which describes my problem
closely.

repSelectorH2 = new Array("test", "usage", "Technical", "individual");

function shortenToggle(jSelector,Selector,showId){
        if (Selector.length != showId.length) {
                alert('numer not equal\n' + Selector.length + "\n" + 
showId.length);
        } else {
                for (var i=0; i<Selector.length; i++) {
                        eval('\$("' + jSelector + '(\'' +  Selector[i] +
'\')").click(function () { \$("' + showId[i] +
'").toggle("fast"); });')
                        }}}



$(document).ready(function(){
shortenToggle('h2:contains',repSelectorH2,repShowIdH2);
});

this code is working fine but smells a bit. here u can see how i
iterate about the different divs.

i look forwad to see your approaches,
 kind regards

On 17 Aug., 22:13, "Michael Geary" <[EMAIL PROTECTED]> wrote:
> It's pretty rare that you actually need to use eval(), and you're right to
> wonder if it is the right approach.
>
> Actually, the code as posted doesn't quite make sense. Your 'selektor'
> variable contains a string 14 characters long. The code doesn't actually
> *do* anything with that string except to use its length in the 'for' loop in
> show(). So, the loop will run 14 times and attach click handlers to the
> first 14 DIVs in your document. Surely that can't be the intent.
>
> Assuming that you want your loop to iterate over all the DIVs and not just
> the first 14, the code can be simplified to:
>
>     $(function(){
>         $('div').each( function( i ) {
>             $(this).click( function() { alert( i ); });
>         });
>     });
>
> This code will be much faster than the original code too.
>
> Next question: Is the alert( i ) just for test purposes, or do you actually
> need the array index in the real code you'll put there?
>
> It's unusual to have any need for the actual index into a jQuery array. In
> most cases there will be something else about the elements that you are
> interested in, not their array indices.
>
> So in most cases, you won't need to even have your own loop at all. Let's
> say it's the innerHTML of each DIV that you want to alert (again for test
> purposes). Then the code can just be:
>
>     $(function(){
>         $('div').click( function() { alert( this.html() ); });
>     });
>
> -Mike
>
> > i have many selectors for the same jquery action.
> > for this pupose i store my selectors in an array and iterate in a loop
> > ever them.
> > to register them im using eval. it is working fine, but is this the
> > right approach?
>
> >    var selektor = '$("div").get()';
> >    var showId = new Array("show1","show2");
>
> >    function show(){
> >            for (var i = 0; i < selektor.length; i++) {
> >                    eval('\$("div:eq(' + i + ')").click(function() {
>
> alert(' + i + ');});');
>
> >            }
> >    }
>
> >    $(document).ready(function(){
> >            show();
> >    });

Reply via email to