Nithin Paul
Nithin Paul

Reputation: 2199

Find and replace width from a HTML text and replace it with new width using jquery

I have one HTML string

Need to update the width attribute with a new value using Jquery

   Example - 
   var str = "Hello <iframe width="560" height="315" src="//www.youtube.com/embed/5Uls9v1nnss" frameborder="0" allowfullscreen></iframe>"

Need to update 560 with new width 460.

Upvotes: 0

Views: 373

Answers (1)

kpull1
kpull1

Reputation: 1663

You can convert any string into html by wrapping it and passing to jQuery function:

var str = "Hello <iframe width=\"560\" height=\"315\" src=\"//www.youtube.com/embed/5Uls9v1nnss\" frameborder=\"0\" allowfullscreen></iframe>";
var container = $("<div>" + str + "</div>");

Then you can process data as normal html:

container.find("iframe").attr("width", "460");

This is the result:

str = container.html();

Try it: http://fiddle.jshell.net/v3019eb4/

Upvotes: 1

Related Questions