SitePoint Sponsor

User Tag List

Page 13 of 13 FirstFirst ... 3910111213
Results 301 to 310 of 310
  1. #301
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Should it be like this.
    Strictly speaking the function should come before where it's used, but broadly speaking, yes.

    Quote Originally Posted by newtomysql View Post
    So how in the handleLocationToUpdate I will refere back to the particular row to decide whether is first or last row or any other row then?
    When the event is triggered, the function is executed within the context of the element that triggered the event. That means, inside the function you use the this keyword to refer to the element that triggered the event. That element being the select drop-down field.

    Once you have the form field, you can get to the row, and from there you can use .prev() or .next() to determine if the row has anything before it or after it, which you can determine by checking the length of the result. If .prev() has a length of 0, there are no other elements before, which means you must be on the first one.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #302
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I have change it like this even then it only called when I change the first two drop down list other no is not called either.
    function handleLocationToUpdate()
    {
    alert("TEst");

    }


    $('table.dynatable tbody tr select[name^="locationFrom"]').change( handleLocationToUpdate);

  3. #303
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    I have change it like this even then it only called when I change the first two drop down list other no is not called either.
    The ones that you added later on don't exist when the page starts. Right? Kind of obvious that.
    So, one thing that can be done is to assign the handler to the appropriate field whenever you create a new row. But there's a better way now available to you.

    jQuery has something called .live() that does that for you, and automatically too.
    It watches for the addition of new elements that match your live selection, and will automatically assign your function to them when they appear.

    So before, you would have had:

    Code javascript:
    $(...).change(handleLocationToUpdate);

    whereas with .live() you would have:

    Code javascript:
    $(...).live('change', handleLocationToUpdate);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #304
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Ok I have done it with live is working well for the rest too now. Now when I change to get the alert values for Next and Prev I always get all 0 for both values after adding even more rows. But for the two rows earlier ones Next is 1 and Prev is 0?

    function handleLocationToUpdate(index)
    {


    alert("Next : "+$(this).next().length);
    alert("Prev : "+$(this).prev().length);
    }


    $('table.dynatable tbody tr select[name^="locationFrom"]').live('change',handleLocationToUpdate);

  5. #305
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Ok I have done it with live is working well for the rest too now. Now when I change to get the alert values for Next and Prev I always get all 0 for both values after adding even more rows. But for the two rows earlier ones Next is 1 and Prev is 0?
    It's not the row that triggers the event, but the drop-down field itself. So, the this keyword within the function will be to that drop-down field.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #306
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Then how to get the row ?

  7. #307
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Then how to get the row ?
    How would you normally traverse from an element to one of its parent elements. Perhaps by using the .closest() method?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #308
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    At last I got it after all the hardwork. Only left one problem I notice is that when I change the second last locationTo then the lastLocationFrom change too just as the other rows but it might not match with the locationTo of the last row then.

    function handleLocationToUpdate(index)
    {
    //alert("Next : "+$(this).closest('tr').next().length);
    //alert("Prev : "+$(this).closest('tr').prev().length);
    if($(this).closest('tr').prev().length>0 && $(this).closest('tr').next().length>0)
    {
    //alert("Middle row");
    var otherField = $('select[name^="locationTo"]', $(this).closest('tr').prev('tr'));
    otherField.val(this.value);
    }
    else
    {
    //alert("First or last row");
    var otherField = $('select[name^="locationTo"]', $(this).closest('tr'));
    otherField.val(this.value);
    }

    }


    $('table.dynatable tbody tr select[name^="locationFrom"]').live('change',handleLocationToUpdate);



    function handleLocationFromUpdate(index)
    {
    //alert("Next : "+$(this).closest('tr').next().length);
    //alert("Prev : "+$(this).closest('tr').prev().length);
    if($(this).closest('tr').prev().length>0 && $(this).closest('tr').next().length>0)
    {
    //alert("Middle row");
    var otherField = $('select[name^="locationFrom"]', $(this).closest('tr').next('tr'));
    otherField.val(this.value);
    }
    else
    {
    //alert("First or last row");
    var otherField = $('select[name^="locationFrom"]', $(this).closest('tr'));
    otherField.val(this.value);
    }

    }


    $('table.dynatable tbody tr select[name^="locationTo"]').live('change',handleLocationFromUpdate);

  9. #309
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,700
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    At last I got it after all the hardwork. Only left one problem I notice is that when I change the second last locationTo then the lastLocationFrom change too just as the other rows but it might not match with the locationTo of the last row then.
    It sounds like you're wanting an additional if statement that checks if the updated field is on the last row, and if so updates things from there, eh?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #310
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Ok I managed it with little if statement there.Here is my link http://183.78.169.54/v3/addRoute.php can you comment if anything is not right. I guess now I can capture via post all the drop down list values right?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •