That's exactly what MorningZ offered:

$("input[id^='item']") will get you all inputs with an #itemXX id,
then you just

$("span[id^='check']").click(function(){
      $("input[id^='item']").ajaxStart(function(){
     })
});

You can achieve the same in a cleaner way using classes.

- ricardo

On Dec 19, 7:02 am, "Carlo Landmeter" <clandme...@gmail.com> wrote:
> Hi,
>
> Thanks for your reply.
>
> The problem isn't how to get the the content out of the input fields.
>
> I have multiple input fields which each have a span which i use for .click.
> I can do what i want for one field and span with the below code.
> If i would like to do this for 10 fields i would have to repeat this code
> below 10x.
> Isn't there an easier way to loop this code with jquery just like i would do
> with php?
>
> I've tried to use .each but i couldn't find out how I could properly replace
> input#item0 with a variable to loop over it.
> I triedhttp://docs.jquery.com/Utilities/jQuery.each#examplesand tried to
> create a var like this:
>
> var foobar = 'input#item' + i;
>
> This didn't seem to do the trick for all vars in my code below.
>
> Does somebody have an example of how i could do this?
>
> Thx,
>
> Carlo
>
> On Fri, Dec 19, 2008 at 2:14 AM, MorningZ <morni...@gmail.com> wrote:
>
> > I'm not 100% clear on what you are trying to accomplish....
>
> > but you can select items by what an attribute "starts with"
>
> >http://docs.jquery.com/Selectors/attributeStartsWith#attributevalue
>
> > so
>
> > $("input[id^='item']")
>
> > would get all your items regardless of how many there are
>
> > On Dec 18, 5:21 pm, "Carlo Landmeter" <clandme...@gmail.com> wrote:
> > > Hi,
>
> > > I'm trying to create a form with multiple input fields. These fields
> > should
> > > have a button to be able to check its content with .ajax.
> > > I am able to create this function for a single field, but i don't know
> > what
> > > the best way would be to do it for multiple input fields:
>
> > > input#item0
> > > input#item1
> > > input#item2
> > > input#item3
> > > .....
>
> > > What would be the best way to create this with jquery so i can adjust a
> > > single var to create if for x items?
>
> > > Thx,
>
> > > Carlo
>
> > > $(document).ready(function(){
> > >   $("span#check0").click(function(event){
> > >     $("input#item0").ajaxStart(function(){
> > >       $(this).addClass("loading");
> > >     });
> > >     $("input#item0").ajaxStop(function(){
> > >       $(this).removeClass("loading");
> > >     });
> > >     $.ajax({
> > >       type: "GET",
> > >       url: "checkitem.php",
> > >       data: {
> > >       'sn': $('input#item0').val()},
> > >       'success': function(msg) {
> > >         if (msg == 'OK')
> > > {$("input#item0").removeClass("nowarranty").addClass("warranty");}
> > >         if (msg == 'NG')
> > > {$("input#item0").removeClass("warranty").addClass("nowarranty");}
> > >       },
> > >       'error': function() {alert('Error: please try again');}
> > >     });
> > >   });
>
> > > });

Reply via email to