I have something like so:
$(document).ready( function() {
var ysr = $('iframe').attr('src');
var nysr = ysr + "HD=1;rel=0;showinfo=0";
$('iframe').attr('src', nysr);});
As Sarfraz already pointed out, the correct way to set the attribute for a jquery selector object is by using the attr("attrName", "attrVal")
. The reason the setAttribute
didn't work is something worth explaining, as I've hit my head against this point on more than one occasion:
When you use jquery's selector syntax, it returns an object -- defined in jquery -- which is essentially a list of all of the elements that the selector matched. Whether it matches one element (which should always be the case with an id selector) or more than one, the returned object is the element list, never a single DOM object (eg single element). setAttribute
is a method for actual HTMLElement
objects.
This is why
$("#frame")[0].setAttribute("width", "200");
works, but
$("#frame").setAttribute("width", "200");
does not. The jquery element does not have that method, even though the HTMLElement
objects in its list do.
If you wanted to (for whatever reason) use a native HTMLElement
method (or a method common to the elements your selector returns, such as inputs, etc), you can use jquery's each()
method, like so:
// Set all iframes to width of 250
$("iframe").each(
function(index, elem) {
elem.setAttribute("width","250");
}
);
The each()
method's callback can be passed two optional parameters, the first being the index of the element in the selector list, the second being the actual DOM element, which you can call native DOM methods on.
Like I said, I've gotten really frustrated trying to figure out how to use jquery's selector results with native methods more than once, so I hope this helps clear up not only why setAttribute()
doesn't work, but native methods in general, and how to actually get them to work when you can't find the jquery equivalent.
1) Using attr:
$("#frame").attr("width", "31");
2) Using DOM object:
$("#frame")[0].setAttribute("width", "50");
What about:
$('#frame').width(x)
or
$('#frame').attr("width","50");
Edit: Saw you do have id. Could you see if the code is actually running? Like is it being triggered? Also, try changing allowfullscreen
to allowfullscreen="true"