SitePoint Sponsor

User Tag List

Page 7 of 13 FirstFirst ... 34567891011 ... LastLast
Results 151 to 175 of 310
  1. #151
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    IS this correct now?I am not sure must I put $ or not in this case?
    The only way that the validate rule works is from the actual name of a form field.
    So, don't you need to obtain the locationFrom and locationTo fields from the row, and to apply the rule separately to each field?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  2. #152
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I am trying to get the value for locationFrom and locationTo as below. I tried this but it does not give me any value why ya? So your idea is send the value and row name is it to the function ?

    $('table.dynatable tbody tr').each(function (index)
    {
    if (index > 0 && $(this).next().length > 0)
    {
    alert("Middle row");
    differentFromAndTo(this);



    // setup rule so that from must not equal to
    }
    else
    {
    alert("First row or last row");
    // setup rule so that from must equal to
    matchingFromAndTo($(this).td:nth-child(2)).val(),$(this).td:nth-child(4)).val());
    //alert($(this).td:nth-child(4)).val(),);
    }
    }
    );

    function differentFromAndTo(row)
    {


    }
    function matchingFromAndTo($first,$second)
    {


    }

  3. #153
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I am trying to get the value for locationFrom and locationTo as below.
    It seems that the code is becoming too confusing for you, so simplify. Don't try to get those fields from outside of the function. Instead, pass the row to the function so that you can then deal with the problem from the less-complicated situation within that function.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #154
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I guess this is what you are trying to tell me.Can you help me on the syntax I am not too sure am I passing correctly the row reference? Just check this function matchingFromAndTo.Thank you.

    $('table.dynatable tbody tr').each(function (index)
    {
    if (index > 0 && $(this).next().length > 0)
    {
    alert("Middle row");
    differentFromAndTo(this);



    // setup rule so that from must not equal to
    }
    else
    {
    alert("First row or last row");
    // setup rule so that from must equal to
    matchingFromAndTo($this);
    }
    }
    );

    function differentFromAndTo(row)
    {


    }
    function matchingFromAndTo($row)
    {
    $(this).td:nth-child(2)).val()==$(this).td:nth-child(4)).val());

    }

  5. #155
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I guess this is what you are trying to tell me.Can you help me on the syntax I am not too sure am I passing correctly the row reference?
    It looks like you're involving far too many dollar symbols in there. Where is $this supposed to retrieve it's value from, and why are you using $row in the function declaration when it's a bog-standard HTML row that's being passed to the function?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  6. #156
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Please correct me the $this I thought is representing the row right? So that is why I am send it like this matchingFromAndTo($this);. So then is this correct to do? So I call as matchingFromAndTo(this);

    function matchingFromAndTo(row)
    {
    row.td:nth-child(2)).val()==row.td:nth-child(4)).val());

    }

  7. #157
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Please correct me the $this I thought is representing the row right?
    It does not. Looking over the documentation when you're unsure about things can be incredibly useful. With the each method for example, you're told that:

    the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

    So instead of $this, which doesn't actually exist, it is the this keyword that is used instead. And if you need to use a jQuery method on it you can wrap it in a jQuery call with $(this) but that's not needed with what you're doing here.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #158
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    So you said pass the row to the function right? So what will this generate $('table.dynatable tbody tr').each(function (index) ? From my understanding it will generate each row right? That row is represented by what then? If I follow this "More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element." What will be the element here the whole row?

  9. #159
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    So you said pass the row to the function right? So what will this generate $('table.dynatable tbody tr').each(function (index) ? From my understanding it will generate each row right? That row is represented by what then? If I follow this "More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element." What will be the element here the whole row?
    That's right, each row will be represented by the this keyword, but only while you are inside the function used by the each method. You pass that this keyword to a separate function, so that you may more easily deal with that row without becoming complicated by other factors.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #160
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    So when I pass is just barely like this is it matchingFromAndTo(this);? Then in the function itself function matchingFromAndTo(row) ? Is this correct?

  11. #161
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    So when I pass is just barely like this is it matchingFromAndTo(this);? Then in the function itself function matchingFromAndTo(row) ? Is this correct?
    That seems right.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  12. #162
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    So can this be part of the function code. Actualyl what exactly do you want to do in this functionMatchingFromAndTo? Just check and send the result if there are similar or not is it?

    function matchingFromAndTo(row)
    {
    row.td:nth-child(2)).val()==row.td:nth-child(4)).val());

    }

  13. #163
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    So can this be part of the function code. Actualyl what exactly do you want to do in this function MatchingFromAndTo?
    The purpose of that function is to accept the row and add a rule to each of the appropriate form fields on that row.

    The from field of that row requires a rule that considers it to be valid only if it matches the to field on that same row.
    The to field of that row requires a rule that considers it to be valid only if it matches the form field of that same row.

    That is my understanding of what you need for those fields. Correct?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  14. #164
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Yes you are correct here. So will my content of the function be something like that? I dont know how to fill this rule[this.name]? Will the below be correct and can you tell me the missing elements here.

    function matchingFromAndTo(row)
    {
    var rule = {};
    rule[this.name] =
    {
    required: function(element) {
    return element.val()==row.nth-child(4).val();
    }
    }
    row.rules('add', rule);
    }

  15. #165
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    It need not be this.name now, because the this keyword now doesn't refer to anything useful.
    The rule index just needs to be the name of the form field that the rule applies to.

    What is a jQuery selector that refers to the from location, from within the context of a certain row?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  16. #166
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    Will this be right. What I try to do first is get the ID and set the name according to the Id of the table column. Then secondly I build the rule for second column. I know will need to do the same for fourth column but if this is correct then I will repeat it for fourth column.

    rule[this.name] = $(row).nth-child(2).attr("id");
    {
    required: function(element) {
    return $(row).nth-child(2).val()==$(row).nth-child(4).val();
    }
    }
    row.rules('add', rule);

  17. #167
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    To reiterate, this.name is useless to you now so it needs to be replaced with the name of the form field that you are adding the rule about.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  18. #168
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I have changed it accordingly now. I get error in this function matchingFromAndTo where row and child is not defined.

    $('table.dynatable tbody tr').each(function (index)
    {
    if (index > 0 && $(this).next().length > 0)
    {
    alert("Middle row");
    differentFromAndTo(this);



    // setup rule so that from must not equal to
    }
    else
    {
    alert("First row or last row");
    //alert("Value : "+$(this).find("td").eq(2).val());
    matchingFromAndTo(this);
    }
    }
    );

    function differentFromAndTo(row)
    {


    }
    function matchingFromAndTo(row)
    {
    alert("ID : "+$(row).nth-child(2).attr("id"));
    rule[$(row).nth-child(2).attr("id")]
    {
    required: function(element) {
    return $(row).nth-child(2).val()==$(row).nth-child(4).val();
    }
    }
    row.rules('add', rule);

    }

  19. #169
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    I have changed it accordingly now. I get error in this function matchingFromAndTo where row and child is not defined.
    Why don't you use a selector instead, with the row as the context within which the selection is chosen?

    $('...', row)
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  20. #170
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I am confuse. So what is the selector method and what is my method? Can you give more hints on how to complete my row value? Thank you.

  21. #171
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    I am confuse. So what is the selector method
    A selector is how you commonly select page elements with jQuery.

    Quote Originally Posted by newtomysql View Post
    and what is my method?
    This is the part that needs to be fixed.

    Code:
    rule[$(row).nth-child(2).attr("id")] = {
        ...
    };
    It needs to be name of a form field that goes there, not an id attribute.
    Those nth-child ones that you are selecting there are going to be TD elements of the trow, rather than input elements instead.

    Quote Originally Posted by newtomysql View Post
    Can you give more hints on how to complete my row value? Thank you.
    I suggest that you use an attribute starts with selector from within the context of the row, so that things can be kept nice and simple.

    That way, you'll only need to use something like this:

    Code javascript:
    rule[$('[name^="locationFrom"]', row).attr('name')] = {
        ...
    };

    Isn't that easier to understand?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  22. #172
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    I have done as below but it gives me error a undefined. Why my this alert("ID : "+$('[name^="locationFrom"]', row).attr('name')); gives me locationFrom[] but why is empty not with the complete row ID.

    function matchingFromAndTo(row)
    {
    alert("ID : "+$('[name^="locationFrom"]', row).attr('name'));
    var rule = {};
    rule[$('[name^="locationFrom"]', row).attr('name')] =
    {
    required: function(element) {
    return $('[name^="locationFrom"]', row).val()==$('[name^="locationTo"]', row).val();
    }
    }
    $(row).rules('add', rule);

    }

  23. #173
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    I have done as below but it gives me error a undefined. Why my this alert("ID : "+$('[name^="locationFrom"]', row).attr('name')); gives me locationFrom[] but why is empty not with the complete row ID.
    It's not working in the same way because that required function gets run long after the other code has finished.

    It's similar to having a completely separate function called validateFromEqualsTo, and you assign that function to the required event:

    Code:
    function validateFromEqualsTo(element) {
        return $('[name^="locationFrom"]', row).val()==$('[name^="locationTo"]', row).val();
    }
    ...
    required: validateFromEqualsTo
    That way it becomes very clear that the function doesn't know anything about the variable called row.

    Do not change your code to match the above code though. The entire purpose of the above code is to demonstrate that the function has no access to the row variable so if you do change your code to use the above code, I will only point you back to this paragraph and tell you that you've done wrong.

    What you do have access to from your function is the element itself. So you only need to use $(element).val() before the comparison sign, but after it you need to use something different instead of row. What you can do is to use jQuery's .parents() method to get from that element up to the 'tr' row.
    Last edited by paul_wilkins; Jun 18, 2011 at 17:39.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  24. #174
    SitePoint Guru
    Join Date
    Feb 2007
    Posts
    874
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dear Paul,
    How about the variable row which we use to assign name for the rule?
    Must we change to element too? I am confuse why some place is ok to use row?

  25. #175
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by newtomysql View Post
    Dear Paul,
    How about the variable row which we use to assign name for the rule?
    Must we change to element too? I am confuse why some place is ok to use row?
    Inside of the matchingFromAndTo function that the variable row was passed to, you can access that row variable.

    The required dependency-callback function is not executed at the same time as the matchingFromAndTo function. It's also not executed within the same context as the matchingFromAndTo function, so you have no access to the variables within that function.

    It's for reasons such as that which is why it helps to pass to a function the variables that it's going to need to work with, instead of making assumptions about what is available.

    The matchingFromAndTo function has passed to it a variable that is called row, and the validation rule passes to the dependency-callback function a variable called element. That's what you have to work with, which is what dictates how the code is written.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •