0

I have an img title attribute that contains the following set of characters: <h1>Image ABC</h1><p>There ist another paragraph</p> (the html tags are neccessary to achieve a certain formatting as the title also serves as the caption of the image, this is a restriction of the CMS)

How can I remove certain characters in the img title attribute using jQuery (especially the html tags)? For example I want to remove everything in the title attribute except Image ABC

Thanks a lot for your ideas!

3
  • What decides what to keep? You can replace the parent container's content with the <h1></h1> if you wish Commented Dec 22, 2014 at 12:39
  • What do you want to get rid of - just all HTML tags and their content? And what have you tried? Commented Dec 22, 2014 at 12:41
  • I tried $("img").prop("title").replace("<h1>", ""); but doesn't work. But the example from @mplungjan worked Commented Dec 22, 2014 at 13:29

3 Answers 3

0

If you mean the title attribute, try this

$(function() {
  $("img[title^='<h1']").each(function() {
    var title = this.title;
    $(this).prop("title",title.substring(0,title.indexOf('</h1>')+5));
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img title="<h1>This will stay</h1><p>This will go</p>" src="http://lorempixel.com/output/abstract-q-c-164-153-6.jpg" />

Or better (no need for parseHTML by the way, as long as we .append or .html it to a tag first)

$(function() {
  $("img[title^='<h1']").each(function() {
    var $title = $('<div/>').html(this.title);
    $(this).prop("title",$title.find('h1').html());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img title="<h1>This will stay</h1><p>This will go</p>" src="http://lorempixel.com/output/abstract-q-c-164-153-6.jpg" />

If you meant an actual title you can do

$(function() {
  $(".title").each(function() {
    $(this).html('<h1>'+$(this).find("h1").html()+'</h1>');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<div class="title"><h1>Image ABC</h1><p>There ist another paragraph</p></div>

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

3 Comments

op is asking for <img title=""> attribute
Thanks a lot @mplungjan. Your first approach was exactly what I needed. Did a small modification and now it gives me the desired output: $(function() { $("img[title^='<h1']").each(function() { var title = this.title; $(this).prop("title",title.substring(4,title.indexOf('<\/h1>'))); }); });
See second example for the H1 content
0

Try this,

HTML

<img title="<h1>Image ABC</h1><p>There ist another paragraph</p>" src=""/>

SCRIPT

 var $img=$('img');
 var $t=$.parseHTML($img.attr('title'));
 // you need to append your title string to output element
 // source http://stackoverflow.com/a/15403888/1817690
 $img.attr('title',$('h1', $('<output>').append($t)).text());

 var $img=$('img');
 var $t=$.parseHTML($img.attr('title'));
 $img.attr('title',$('h1', $('<output>').append($t)).text());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img title="<h1>Image ABC</h1><p>There ist another paragraph</p>" src=""/>

5 Comments

Clever. I have not seen parseHTML before. Is there an actual reason to use it?$('<output>').append($img.prop("title")) should get the same, no?
I would want to test that.
This worked for me without parseHTML: var $title = $('<div/>').html(this.title);
But you need a div element now, you have to use that div to get h1 text which may affect the DOM and in my case I used a dummy element output which has only significance to get the h1 element from the title string without affecting DOM.
I fully expect var $title = $('<dummy/>').html(this.title); to work too
-1

You can replace some specific words with the replace function of Javascript:

var $data = "<h1>Image ABC</h1>";
$data.replace("ABC","");

Maybe if your tags dont have ids or unique identifier you cann use .each

$(function() {
  $(".title").each(function() {
    $(this).html('<h1>'+$(this).find("h1").html()+'</h1>');
  });
});

1 Comment

Thx, @Mike, for your idea. I went with the solution from mplungjan.

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.