Using Jquery ,
I have an array result
[<a href=""><img src="image1"></a>,<a href=""><img src="image2"></a>]
if I try to do each I only get the first one in array ,
how could I split this so I could do
$.each(my_array, function (index, value) {
this.parent().attr.('href',this.src);// assign image as href to parent
});
here is bad try
Please note that array above is returned to my by imagesLoaded plugin for jquery and I canot select parent directly since it is not within the result , ZI must go by element.parent() any help is appreciated. thnx!
5 Answers 5
Given the following HTML:
<a href=""><img src="image1"></a><a href=""><img src="image2"></a>
And a jQuery selector which catches only these elements, in this case simply:
var my_array = $('a');
You would do:
$.each(my_array, function(i, el) {
el.href = $(el).children('img').attr('src');
});
If your selector is on the img tags:
var my_array = $('img');
You would do:
$.each(my_array, function(i, el) {
$(el).parent().attr('href', el.src);
});
3 Comments
my_array is actually a jQuery object such as is returned by $('img') then you wouldn't use $.each(my_array, function(i, el) {}) when you can say my_array.each(function(i, el) {}).Select the img elements.
var $imgs = $("a > img");
Loop over selected elements
$imgs.each(function () {
$(this).parent().attr('href',this.src);// assign image as href to parent
});
Note there is no '.' after attr, since that is a method. Also, you need to do $(this), since this in the loop is a dom element, not a jquery object.
3 Comments
$imgs is; it's a reference to the jquery object which matches the selector -- a reference to the and the .each method on a jquery object loops through matched elements and passes them into the loop as this. Doing $(this) gives you a jquery reference to the object, so you can't possibly see an issue of image element not having method parent, like you had originally with only this.var arr = ['<a href=""><img src="image1"></a>','<a href=""><img src="image2"></a>'];
$.each(arr, function (index, value) {
alert(value.replace(/^.*src="(.*)".*$/m, '1ドル'));
});
2 Comments
Use this :
var obj = ['<a href=""><img src="image1"></a>,<a href=""><img src="image2"></a>']
jQuery(obj).each(function(key,value){
var imgObj = jQuery(value).find('img')
jQuery(imgObj ).each(function(key,value){
jQuery(this).parent('a').attr('href',jQuery(this).attr('src'));
});
});
Here is the DEMO
Comments
if your code is: http://jsfiddle.net/ZZVXf/6/ then you just need to change it to:
$.each($('img'), function (index, value) {
$(this).parent().attr('href', this.src);// assign image as href to parent
});
because you want to access the jQuery methods you need to wrap this with jQuery (witch is the DOM reference, not a jQuery reference).
thisis the DOM reference, and you are trying to useparent()witch is a jQuery method, you need to wrapthiswith jQuery to access jQuery methods. and this will refer to the anchor tag, so you need to goto the image element first before trying to access its src path.