Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:32:39+00:00 2026-06-15T14:32:39+00:00

I have some duplicate code like below, I want remove the duplicate code, to

  • 0

I have some duplicate code like below, I want remove the duplicate code, to achieve “DRY” (Do not Repeat Yourself) principle. Anybody can help me out will be great appreciated!

Javascript code is like below:

<script type="text/javascript">
    $(document).ready(function () {

        $(".adjustLineFeedProductName").ready(function () {
            var str = "";
            for (i = 0; i < $(".adjustLineFeedProductName").html().length; i += 66) {
                str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + $(".adjustLineFeedProductName").html().substring(i, i + 66) + "</p>";
            }
            $(".adjustLineFeedProductName").html(str);
        });

        $(".adjustLineFeedQuantityPerUnit").ready(function () {
            var str = "";
            for (i = 0; i < $(".adjustLineFeedQuantityPerUnit").html().length; i += 66) {
                str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + $(".adjustLineFeedQuantityPerUnit").html().substring(i, i + 66) + "</p>";
            }
            $(".adjustLineFeedQuantityPerUnit").html(str);
        });

    });
</script>

Html code is like below:

<div class="elaborate" style="margin: 0px 0px 0px 0px; padding: 0px 0px 3px 10px;"><b class="adjustLineFeedProductName"><%: (string)Model.Product.ProductName %></b></div>
<div class="elaborate" style="margin: 0px 0px 0px 0px; padding: 3px 0px 0px 10px;"><span class="adjustLineFeedQuantityPerUnit">Quantity/Unit : <%: (string)Model.Product.QuantityPerUnit %></span></div>

I want using below Javascript to instead above Javascript, but it is not working:

<script type="text/javascript">
    $(document).ready(function () {
        $(".adjustLineFeed").ready(function () {
            var str = "";
            for (i = 0; i < this.html().length; i += 66) {
                str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + this.html().substring(i, i + 66) + "</p>";
            }
            this.html(str);
        });
    });
</script>

I tried using below html code instead above html code, but it is not working:

<div class="elaborate" style="margin: 0px 0px 0px 0px; padding: 0px 0px 3px 10px;"><b class="adjustLineFeed"><%: (string)Model.Product.ProductName %></b></div>
<div class="elaborate" style="margin: 0px 0px 0px 0px; padding: 3px 0px 0px 10px;"><span class="adjustLineFeed">Quantity/Unit : <%: (string)Model.Product.QuantityPerUnit %></span></div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:32:40+00:00Added an answer on June 15, 2026 at 2:32 pm

    Inside a jQuery block, this will refer to the HTMLDomElement instance. On this you can do DOM like operations such as innerHTML etc, but obvious you can’t do jQuery methods as.. it isn’t a jQuery object.

    To create a jQuery object, pass the DOM element to $ like so;

    $(this)
    

    Some other points;

    1. $(".adjustLineFeedProductName").ready() is meaningless. A span or a br is never ready; I’m suprised this even works. Having your code inside a $(document).ready() is enough. What you probably want is to use jQuery.each() so you can target all instances of .adjustLineFeedProductName and .adjustLineFeedQuantityPerUnit on your page (if there is only one instance, consider an ID instead).

      $(".adjustLineFeedProductName").each(function () {
          var str = "";
          for (i = 0; i < $(this).html().length; i += 66) {
              str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + $(this).html().substring(i, i + 66) + "</p>";
          }
          $(this).html(str);
      });
      
    2. Cache the response of $(). $(this). $() does work behind the scenes each time you call it. Whilst it’s not a great deal of work for DOMElements, it’s a good practice to get into the habit of caching these. jQuery selectors, for example, perform DOM lookups which aren’t trivial.

      $(".adjustLineFeedProductName").each(function () {
          var str = "";
          var self = $(this); // cache
      
          for (i = 0; i < self.html().length; i += 66) { // use self
              str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + self.html().substring(i, i + 66) + "</p>"; // use self
          }
          self.html(str);
      });
      
    3. Similar to #2, cache html() as well, for the same reasons.

       $(".adjustLineFeedProductName").each(function () {
          var str = "";
          var self = $(this); // cache
          var html = self.html();
      
          for (i = 0; i < html.length; i += 66) { // use self
              str = str + "<p style=\"margin: 0px 0px 0px 0px;\">" + html.substring(i, i + 66) + "</p>"; // use self
          }
          self.html(str);
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Ternary conditional operator in Python If I have some code like: x
I have a Windows Phone 7.5/Silverlight app. I have some code that I duplicate
Say if I have the code below, it's bascially determining some condition is matched
I want to remove some duplicate values on an array, but there is a
I have some Code that I'd like to share between an iOS and an
In order to do not duplicate code, I've decided some time ago, to refactor
I have some source code and I would like to auto generate class diagrams
I have a tables 'players' and 'awards'. I have some duplicate player entries that
Possible Duplicate: Default textbox border-style and width I have some TextBox on my page
Possible Duplicate: Is there any working memory profiler for Python3 I have some script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.