1

This piece of code appears in a js script I have been asked to modify - I'm not sure why it is written in this way, it doesn't make sense to me.

Can anyone help explain what it is doing, and if it can be simplified to be a little more meaningful?

 var unformathtml = $(this).text();
 if(unformathtml.trim().length>showChar) {
        $(this).parent().parent().parent().parent().parent().find('.comment-footer').fadeOut();
 }
5
  • where is this code exactly and what app are you modifying? Commented May 17, 2017 at 19:23
  • 1
    If the total of characters that should be shown showChar is greated than the ones in the element this, it will hide with an animation the .comment-footer. Commented May 17, 2017 at 19:24
  • 1
    Well, the multiple .parent() can be reduced to a .closest() is you have something to identify the ancestor tag, or a .parents().eq(4) which is not so clean and good performance wise. As for what it does, it simply hide (with a fading animation) something if the number of chars is higher than something... Commented May 17, 2017 at 19:25
  • It would be helpful if you'd include the HTML on which this code is running, so that we don't have to guess (as others have done). Commented May 17, 2017 at 19:34
  • I Highly recommend reading Philip Walton (Engineer @ Google) - Decoupling Html CSS and Javascript. Commented May 17, 2017 at 19:48

2 Answers 2

2

Lets' pretend we have a DOM like this:

<parent-5>
  <target-element>Content</target-element>
  <parent-4>
    <parent-3>
      <parent-2>
        <parent-1>
          <focused-element>Some Text</focused-element>
        </parent-1>
      </parent-2>
    </parent-3>
  </parent-4>
</parent-5>

What this code is saying is "if the text inside of <focused-element> has more characters than showChar then fade out <target-element>.

A better way of doing this would be to give <parent-5> some kind of identifier, which could be an ID or a class, and target that instead of the repeated .parent() call.

Here's an example which showcases the idea:

$('#oldMethod').click(function() {
  $(this)
    .parent()
    .parent()
    .parent()
    .parent()
    .parent()
    .find('.comment-footer')
    .toggleClass('red');
});

$('#newMethod').click(function() {
  $(this)
    .closest('.comment-container')
    .find('.comment-footer')
    .toggleClass('red');
});
.red {
  color: #F00;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="comment-container">
  <div>
    <div>
      <div>
        <div>
          <button id="oldMethod">Old</button>
          <button id="newMethod">New</button>
        </div>
      </div>
    </div>
  </div>
  <div class="comment-footer">Footer</div>
</div>

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

Comments

1

Wow, that really doesn't make much sense. It is doing this:

1) Getting the raw contents out of an element 2) Checking to see if it is longer than a certain length 3) If so, fading out another element on the page

The parents() thing is very error-prone. It is going up a very precise number of levels in the HTML tree and then descending downwards to find an element with a class of '.comment-footer'. As a result a slight rearrangement of either element in the DOM might result in the code no longer working, because it can't find the specified element.

What you want is to find the tag-to-hide more directly. Ideally, the element-to-hide and the element-that-decides-to-hide would be next to eachother in the DOM (i.e. the element being hidden would be a child or sibling of the element that decides whether or not to hide it). This makes it very easy for the one to find the other. If that isn't possible, your next best bet would be to simply assign an id to the element you are trying to hide and then select on that id directly:

 var unformathtml = $(this).text();
 if(unformathtml.trim().length>showChar) {
        $('#to_hide').fadeOut();
 }

As a quick aside, .text() is used (instead of .html()), because the former removes any HTML tags. This way you are measuring the amount of "actual" text inside $(this) to determine whether or not you want to hide said element. So that part is probably fine.

Comments

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.