Remove HTML tags from image title and Alt attribute using jQuery


Jason

I have a client using a CMS and the title and alt image attributes have been pulled from the title. Most headers have html (span and br tags) in them. Do I need to remove the html tags from the title and alt text in the following sample code?

<ul class="products">
    <li><img src="images/product_image_1" title="<span>Company Name</span> First Product<br /> Name" /></li>
    <li><img src="images/product_image_2" title="<span>Company Name</span> Second Product<br /> Name" /></li>
</ul>

I need it to look like this:

<ul class="products">
    <li><img src="images/product_image_1" title="Company Name First Product Name" /></li>
    <li><img src="images/product_image_2" title="Company Name Second Product Name" /></li>
</ul>
David says to restore Monica

I recommend:

// iterate over each of the <img> elements that have a
// title attribute:
$('ul li img[title]').each(function() {
  // set the title of the current <img> to
  // the text returned from a dummy <div> element,
  // after setting its html to that of the title attribute:
  this.title = $('<div />', {
    'html': this.title
  // finding the text of that created-<div>:
  }).text();
});

$('ul li img[title]').each(function() {
  this.title = $('<div />', {
    'html': this.title
  }).text();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="products">
  <li>
    <img src="http://lorempixel.com/200/200/nightlife" title="<span>Company Name</span> First Product<br /> Name" />
  </li>
  <li>
    <img src="http://lorempixel.com/200/200/people" title="<span>Company Name</span> Second Product<br /> Name" />
  </li>
</ul>

refer to:

Related


Remove HTML tags from image title and Alt attribute using jQuery

Jason I have a client using a CMS and the title and alt image attributes have been pulled from the title. Most headers have html (span and br tags) in them. Do I need to remove the html tags from the title and alt text in the following sample code? <ul class="

Remove HTML tags from image title and Alt attribute using jQuery

Jason I have a client using a CMS and the title and alt image attributes have been pulled from the title. Most headers have html (span and br tags) in them. Do I need to remove the html tags from the title and alt text in the following sample code? <ul class="

Strip HTML tags from title and alt attributes of image tags

Tustinde In some of our articles we have images that incorrectly have links hardcoded into the title/alt attributes of the image tags, which breaks the display of the images. E.g: <img src="/imgs/my-image.jpg" title="This is a picture of a <a href="/blob.html"

Remove tags from html string using Jquery

username I am trying to remove a span class from an HTML string using Jquery. My HTML string looks like this: <li class="left clearfix"><span id="userAvatar" class="chat-img pull-left"><img src="http://placehold.it/50/55C1E7/fff&amp;text=U" alt="User Avatar" c

Remove tags from html string using Jquery

username I am trying to remove a span class from an HTML string using Jquery. My HTML string looks like this: <li class="left clearfix"><span id="userAvatar" class="chat-img pull-left"><img src="http://placehold.it/50/55C1E7/fff&amp;text=U" alt="User Avatar" c

Remove html tags from xpath @attribute

Recco I'm trying to extract some text from a webpage using lxml and xpath - I need two bits Main text body: page = requests.get(url) pageopen = lxml.html.fromstring(page) body_one = pageopen.xpath('/html/body//div/div/div//div/p[@class="body"]/text()') very

Remove html tags from xpath @attribute

Recco I'm trying to extract some text from a webpage using lxml and xpath - I need two bits Main text body: page = requests.get(url) pageopen = lxml.html.fromstring(page) body_one = pageopen.xpath('/html/body//div/div/div//div/p[@class="body"]/text()') very

Remove style tags from HTML page using jQuery

Krishna Is there any way to remove tags <style>from a page using jquery ? And I need to remove specific style tags. thanks. Rituraj Ratan $(document).ready(function(){ $('style').detach(); // or $('style').remove(); }); See .remove() and .detach(

jQuery tooltip from html title attribute

tangerinr I've been trying Bootstrap's jQuery Tooltip , which requires a lot of extra properties: <a rel="tooltip" data-placement="right" data-original-title="Hello" href="#">Tooltip Example</a> This seems to be a bit of a problem though, as I'm using rel="no

jQuery tooltip from html title attribute

tangerinr I've been trying Bootstrap's jQuery Tooltip , which requires a lot of extra properties: <a rel="tooltip" data-placement="right" data-original-title="Hello" href="#">Tooltip Example</a> This seems to be a bit of a problem though, as I'm using rel="no

Get image tags from html and string content using JQuery

Yagnesh Balala Get image tags from html and string content using JQuery. I guess I need to image the tag form content and replace it with its alt attribute. for. example: Given my html string content. Hello this is long content with multiple images <img alt=":

Remove attached image from jQuery dialog title

Jane S I've looked into various solutions on StackOverflow but haven't had any success getting them to work. I can easily add an image to the dialog, but various attempts to remove the image have failed, so I get multiple images. Note that I don't want it to a

Remove attached image from jQuery dialog title

Jane S I've looked into various solutions on StackOverflow but haven't had any success getting them to work. I can easily add an image to the dialog, but various attempts to remove the image have failed, so I get multiple images. Note that I don't want it to a

Alt, title and other tags in email html

em2 Does it make sense to put alt, title and other tags in the HTML or elements of an email? Google Pretty Reads All Gmail And since my clients often send emails with products, does it make sense to put tags in elements to help Google understand what we're sen

Alt, title and other tags in email html

em2 Does it make sense to put alt, title and other tags in the HTML or elements of an email? Google Pretty Reads All Gmail And since my clients often send emails with products, does it make sense to put tags in elements to help Google understand what we're sen

How to extract img src, title and alt from HTML using php?

Sam I want to create a page where all the images present on my website are listed with a title and an alternate representation. I've written me a small program to find and load all the HTML files, but now I'm stuck on how to extract and srcbase this HTML:title

How to extract img src, title and alt from HTML using php?

Sam I want to create a page where all the images present on my website are listed with a title and an alternate representation. I've written me a small program to find and load all the HTML files, but now I'm stuck on how to extract and srcbase this HTML:title

How to extract img src, title and alt from HTML using php?

Sam I want to create a page where all the images present on my website are listed with a title and an alternate representation. I've written me a small program to find and load all the HTML files, but now I'm stuck on how to extract and srcbase this HTML:title

jQuery remove error tags from HTML blocks

royal dad I am trying to remove error tags from my html when the field content changes. It works fine on inputand on select, but not on input-group. I want a command that can be run on all HTML instances. My problem here is a JSFIDDLE . my HTML: <div class="co

Remove disabled attribute from HTML object in jQuery

Developer I add html to the page on the click of a button. So when I add html, I want to remove all disabled attributes from the html variable so that the new html doesn't have any disabled inputs: Code: var current_td = $(thisobj).closest('tr').html(); va

Remove disabled attribute from HTML object in jQuery

Developers I add html to the page on the click of a button. So when I add html, I want to remove all disabled attributes from the html variable so that the new html doesn't have any disabled inputs: Code: var current_td = $(thisobj).closest('tr').html(); v

Remove html tags from string using jQquery

Habyat I want to remove HTML tags like remove div, p, br, ... from a string I am trying to do this: var mystring = "<div><p>this</p><p>is</p><p>my</p><p>text</p><p>sample</p><p> </p><p> </p></div>" var html3 = $(mystring).text(); But the result is: "thisismy

Remove html tags from string using jQquery

Habyat I want to remove HTML tags like remove div, p, br, ... from a string I am trying to do this: var mystring = "<div><p>this</p><p>is</p><p>my</p><p>text</p><p>sample</p><p> </p><p> </p></div>" var html3 = $(mystring).text(); But the result is: "thisismy