1

I'm trying to show the difference in days between two input fields that contain dates. I am pretty much unable to alter the HTML so I have to do this with Javascript. The HTML is pretty ugly. I can only use jQuery 1.3.2.

HTML:

<div id="longUnreadableUnidentifiableId">
  <div class="dynamic">
    <div class="row">
      <span class="column">
        <input id="horribleHorribleLongIdTxtFrom" type="text">11/02/2002</input>
      </span>
      <span class="colum">
        <input id="horribleHorribleLongIdTxtUntil" type="text">16/12/2010</input>
      </span>
      <div class="diff"> [diff] </div>
    </div>
  </div>

<div id="longUnreadableUnidentifiableId2">
  <div class="dynamic">
    <div class="row">
      <span class="column">
        <input id="horribleHorribleLongIdTxtFrom" type="text">03/03/2003</input>
      </span>
      <span class="colum">
        <input id="horribleHorribleLongIdTxtUntil" type="text">16/12/2010</input>
      </span>
      <div class="diff"> [diff] </div>
    </div>
  </div>

When any one of the dates on a certain line change, the difference between them should show. I'm using moment.js for the date diff.

The following javascript tries to find the associated input field and alert the difference between them. However, I am having trouble finding the second input field (var until).

Javascript:

$('input[id*="txtFrom"]').live('change', function() { 
    var from = $(this);
    var until = from.parent(".row").find('input[id*="txtUntil"]');

    a = moment(from.val());
    b = moment(until.val());

    alert(b.diff(a, 'days'));
});

Does someone have a solution to this problem? Perhaps something more elegant?

3
  • 3
    You biggest issue seems to be: <<I can only use jQuery 1.3.2>> Commented Jul 17, 2013 at 10:16
  • Could you expand on why you are constrained to using such an old version of jQuery? Commented Jul 17, 2013 at 10:17
  • The site on which I'm making this change is pretty significant and I cannot upgrade jQuery in case of breaking changes. I do not have the time or resources to properly test it. The decision is not mine. Commented Jul 17, 2013 at 10:19

2 Answers 2

3

Try using parents instead of parent. parent just checks the immediate parent, whereas parents checks all of them.

Sign up to request clarification or add additional context in comments.

Comments

1

Some suggestions:

  1. Correct your html syntax, use

    <input id="horribleHorribleLongIdTxtFrom" type="text" value="11/02/2002"></input>
    

    instead of

    <input id="horribleHorribleLongIdTxtFrom" type="text">11/02/2002</input>
    
  2. your element id horribleHorribleLongIdTxtFrom coming twice in same page, you are breaking the html rule.

3 Comments

(1) Are you sure? It works here... (2, 3) I have no way to edit the HTML, and yes, it is indeed breaking the HTML rule. Old code :(
@kabforks: pardon bro, it's my mistake about (1). That's y I removed
<input ...> tags don't need closing </input> tags, see stackoverflow.com/questions/13232121/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.