0

I'm trying to implement some Javascript that will remove an element and its parent from a page based on whether the date inside the element has gone by.

If for example todays date is December 16, 2014 and the element contains the date December 15, 2014 than I want the element to be removed. I already have some code that I used for a different site of mine that removes the element and it's parent successfully, however the date format for that site is different than the date format on the current site. Applying this code doesn't work for the current site.

Here's the Javascript I have from the other site for removing the element:

<script type='text/javascript'>

    jQuery(document).ready(function(){
        jQuery('.o-date').each( function(key,value){
                console.log(value);
            var currentDate = new Date();
            var date = new Date(jQuery(value).text());
            if(date < currentDate){
                jQuery(value).parent().remove();
            }
        });
    });

</script>

Here's the markup on the current site for the element containing the date and it's parent:

<tr data-offer-id="13503" class="special">
    <td class="o-date">December 14, 2014 - January 9, 2015</td>
    <td class="o-content"></td>
    <td class="o-amount">
        <select>
            <option value="0">0</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
    </td>
</tr>

EDIT: Here is a jsfiddle for this.

4
  • can you put this in jsfiddle
    – Todd
    Commented Dec 17, 2014 at 9:32
  • is currentDate meant today? Commented Dec 17, 2014 at 9:36
  • You want to remove the item if one of the two date is before today ? In your example, you want to remove the item although the second date is in the future ?
    – Matthieu
    Commented Dec 17, 2014 at 9:37
  • Yes, I would like to remove the item even though the second date is in the future. Commented Dec 17, 2014 at 9:44

1 Answer 1

1

assuming they're all formatted in this way, and also that you want to remove only if the first of two dates has passed:

the js fiddle was acting weird, so i made a codepen instead. I also had to ad a table around those <tr/> els.

DEMO

jQuery(document).ready(function(){
    jQuery('.o-date').each( function(i,val){
        var currentDate = new Date();
        var date = new Date(jQuery(val).text().split(' - ')[0]); // or 1 for second date
        if(date < currentDate){
            jQuery(val).parent().remove();
        }
    });
});
7
  • nice job! i was about to post the exact same solution a few seconds later :)
    – low_rents
    Commented Dec 17, 2014 at 9:38
  • just not sure if OP wants to compare to the first or the second date.
    – low_rents
    Commented Dec 17, 2014 at 9:40
  • Hi, thanks for this. I would like to remove the entire element if just one of the dates has past. Also I tried to implement your solution but I couldn't get it to work. Could you possibly apply it to this jsfiddle: jsfiddle.net/ciarangaffey/s17w4uL2. I'd greatly appreciate it. Thanks! Commented Dec 17, 2014 at 9:41
  • I added codepen because jsfiddle was acting strangely. see edited.
    – Todd
    Commented Dec 17, 2014 at 9:50
  • since you want to only check if one has passed, you'll want to go with 0 instead of 1. I'll reflect change in answer.
    – Todd
    Commented Dec 17, 2014 at 9:51

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.