Issue with h2 tag within JQuery's read more feature

How can I modify my jQuery code to wrap the middle text in an h2 tag?

I am currently using a code snippet from code-tricks. You can find the original code snippets here:

$(document).ready(function() {
  var showChar = 100;
  var ellipsestext = "...";
  var moretext = "Show more >";
  var lesstext = "Show less";


  $('.more').each(function() {
    var content = $(this).html();

    if (content.length > showChar) {

      var c = content.substr(0, showChar);
      var h = content.substr(showChar, content.length - showChar);

      var html = c + '<span class="moreellipses">' + ellipsestext + '&nbsp;</span><span class="morecontent"><span>' + h + '</span>&nbsp;&nbsp;<a href="" class="morelink">' + moretext + '</a></span>';

      $(this).html(html);
    }

  });

  $(".morelink").click(function() {
    if ($(this).hasClass("less")) {
      $(this).removeClass("less");
      $(this).html(moretext);
    } else {
      $(this).addClass("less");
      $(this).html(lesstext);
    }
    $(this).parent().prev().toggle();
    $(this).prev().toggle();
    return false;
  });
});
.morecontent span {
  display: none;
}
.morelink {
  display: block;
}
.productdescription {
  font-weight: 400;
}
.productdescription h2 {
  font-weight: normal;
  font-size: 13px;
  margin: 0px;
  padding-top: 24px;
}
.productdescription strong {
  padding-bottom: 24px;
  float: left;
  width: 100%;
}
.productdescription ul {
  list-style-type: square;
  padding: 24px 0px 0px 15px;
}
.morecontent span {
  display: none;
}
.morelink {
  display: block;
}
.morelink {
  color: #4285f4;
  display: inline-block;
}
.morelink:hover {
  text-decoration: underline;
}
.less {
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<span class="productdescription more">
<strong>Produktdetails</strong> 
  
Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. Change this text to a headline h2. 
  
  

<ul>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
<li>Lorem ipsum dolor sit amet</li>
</ul>
</span>

Answer №1

Place the following text within a tag that has an identifier:

<div id="newId">
Transform this text into a h2 heading. Transform this text into a h2 heading. Transform this text into a h2 heading. Transform this text into a h2 heading. Transform this text into a h2 heading. Transform this text into a h2 heading. Transform this text into a h2 heading. 

</div>

Afterwards, utilize jQuery .html()

$("#newId").html("<h2>Modify this text... </h2>");

Answer №2

This method offers a unique approach to implementing the hide/show more text feature without the need for Javascript. To achieve this, you'll have to get creative with your styling and positioning to achieve the desired look. I've put together a simple demonstration using a checkbox for toggling content visibility, leveraging the :checked CSS3 pseudo-class selector.

<div class="snippet" data-lang="js" data-hide="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>    #more {
  display: none;
}
label {
  float: right;
}
.morecontent {
  display: inline-block;
  height: 20px;
  width: 200px;
  overflow: hidden;
}
#more:checked ~ .morecontent {
  height: auto;
  overflow: visible;
}
label+label {
  display: none;
}
#more:checked + label {
  display: none;
}
#more:checked + label + label {
  display: inline;
}
.morecontainer {
  width: 250px;
}
.morecontent p {
  margin-top: 0;
}
<div class="morecontainer">

  <input type="checkbox" id="more" />
  <label for="more">...more</label>
  <label for="more">...less</label>
  <div class="morecontent">
    <p>This is the container that is supposed to have that more text functionality applied to it.</p>
    <ul>
      <li>This solution requires no Javascript.</li>
      <li>You can use a tiny little bit of JS for pure animation purposes.</li>
    </ul>
  </div>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

HTML: arranged <pre> with fixed positioning

I currently have a centered column of text with a fixed width. However, I am looking to break that fixed width for certain tags like <pre> so that they can fill the full screen width while maintaining flow with the rest of the text. My CSS snippet s ...

Creating impenetrable div elements with JavaScript or jQuery

I'm currently working on creating solid blocks using DIVs positioned side by side both horizontally and vertically. I've successfully achieved this when the divs have equal heights. However, an issue arises when a div has larger dimensions; it en ...

Ensuring jQuery filter() works seamlessly with Internet Explorer versions 6, 7, and 8

On my webpage, I have implemented an ajax call using the jQuery library to handle a specific task. After making the ajax call, I need to parse the response message that is returned. However, I encountered an issue with Internet Explorer versions 6, 7, and ...

Having trouble getting anime.js to function properly in an Ionic 3 project?

I have been attempting to incorporate anime.js into my Ionic 3 project, but I keep encountering an error when using the function anime({}) in the .ts file. Error: Uncaught (in promise): TypeError: __webpack_require__.i(...) is not a function TypeError: _ ...

Effortlessly transfer files with Ajax through Box

I attempted to utilize the Box.com API for file uploads according to instructions from https://gist.github.com/seanrose/5570650. However, I encountered the following error message: `XMLHttpRequest cannot load "". No 'Access-Control-Allow-Origin&ap ...

Attempting to align content in the middle of the page across all web browsers

I need assistance with centering all the content on my website across different screen sizes and browsers. Currently, everything appears off-center and aligned to the left on various screens I have tested. You can view the HTML and CSS code in this link. H ...

What is the reason for nth-of-type selectors not functioning on the third element?

I have a question regarding CSS nth-of-type. In the following code snippet, nth-of-type(2) and nth-of-type(3) are functioning properly, however, nth-of-type(4) and nth-of-type(5) are not working as expected. Could there be an issue with my code? <div i ...

How can I determine if a URL in react js includes a specific string?

I am working on a project where I need to check if a given URL contains the string youtube using react hooks in React JS. This is what my current code looks like: useEffect(() => { let srcLink = "www.google.com/https://img.youtube.com/vi/h9-qcy3HQn ...

jQuery.ajax Failure Response

When using MVC on the server side and calling a function with jQuery.Ajax that returns JSON, an exception is being thrown. How can I invoke the error handling function of Ajax by sending something back in the return JSON function? MVC Function public Jso ...

What methods can I use to stop the styles of a child component from impacting those of the parent component

Is there a way to create CSS rules that achieve the following conditions? The parent component's CSS does not impact the child component The child component's CSS does not affect the parent component The child component is sourced from an exte ...

Embrace the words enveloped within large quotation marks

I am seeking a way to format paragraphs with large quotation marks surrounding them. I have attempted several methods, but my line-height seems to be affected, as shown in the following image: Can anyone suggest a proper method for achieving this? (Addit ...

Step-by-step guide to implementing a user-friendly search input field using the powerful AngularJS material design framework

I am searching for an effortless method to implement a feature similar to the expandable search text field in angular-mdl. By clicking on a search button, it will expand into a text field. <!-- Expandable Textfield --> <form action="#"> < ...

Template displaying multiple polymer variables side by side

Here is the date object I am working with: date = { day: '05' } When I use this code: <div>{{date.day}}</div> It generates the following HTML output: <div>05</div> Everything looks good so far. Now, I want to try th ...

In my Angular application, I have two div elements that I want to toggle between. When a button located in the first div is clicked, I need

I am working on a code snippet that requires me to hide div1 and display div2 when the button in div1 is clicked using Angular HTML5. Currently, I have two separate modal pop up template files and JS controllers for each of them. Instead of having two po ...

Establishing a default value for sj:select

As I utilize the struts2-jquery plugin, I am faced with the need to establish a default value for the sj:select drop-down tool. The current functionality of the widget is operational, displaying all relevant values (retrieved from a map, where the value c ...

Experiencing a blank array when using filtering/search text in a Nodejs application with MongoDB

I am experimenting with search functionality in a MongoDB database using Node.js. However, my result array is always empty. I have shared my code here and would appreciate some assistance in identifying the issue. Whenever I perform a search, I end up with ...

Utilizing jQuery tabs to dynamically load content via URL links

Here is the code for my script: function() { $( "#tabs" ).tabs(); }); function LoadContent(link, div_id, param1, param2, param3) { $.ajax(link, { data: { par1: param1, par2: param2, par3: param3 }, type: "post", success: function(data) { ...

Text wrapped automatically to display multiple lines with spacing between each highlighted line

Question about CSS formatting and spacing. I need to automatically break sentences into new lines based on the width of the container. For example, a sentence like: This is a sentence should be displayed as: This is<br>a sentence I am trying ...

Creating animated direction indicators in the "aroundMe" style with ngCordova

I am attempting to recreate a compass or arrow similar to the one featured in the AroundMe Mobile App. This arrow should accurately point towards a pin on the map based on my mobile device's position and update as I move. I have been struggling to fi ...

Is it possible to transform this nested promise into a chain instead?

This situation is just a scenario | then (items) | then (items, actions) getItems() | getActions(for:items) | apply(actions -> items) :promise | :promise | model <= items | | :sync ...