toomanyairmiles
toomanyairmiles

Reputation: 6485

Strip html from the title="" and alt="" attributes of a link based on a css class

I need to strip html from the title="" and alt="" attributes of a link carrying a specific css class.

The code is being generated by a CMS and having no access to the page code, only the head, I'm thinking jQuery might be a good solution.

I've got this far

function removeAllHtml() {
    $(".example_1").html( $(".example_1").text() );
};
removeAllHtml();

But I have no idea how to limit it based on a css class or to specify only the alt and title attributes. Can anyone help? Thanks in advance.

Upvotes: 0

Views: 591

Answers (2)

David Thomas
David Thomas

Reputation: 253318

You could use:

$(".example_1").each(
    function(){
        $(this).attr({'title' : '', 'alt' : ''});
    });

Demo at JS Fiddle.

Upvotes: 2

tvanfosson
tvanfosson

Reputation: 532495

I'm assuming that you want to have the title and alt tags cleaned of any HTML. You could iterate over all the elements with the class, replacing the title and alt attributes of each with the text extracted from the existing element.

$(function() {
    $('.someclass').each( function() {
        var $this = $(this),
            title = $this.attr('title'),
            alt = $this.attr('alt');
        $this.attr('title',$(title).text())
             .attr('alt',$(alt).text();
    });
});

Upvotes: 2

Related Questions