0

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

http://jsfiddle.net/ZZVXf/6/

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!

asked Jun 21, 2012 at 6:17
4
  • yes thnx fixed but still same issue Commented Jun 21, 2012 at 6:20
  • @Baz1nga [<a href=""><img src="image1"></a>,<a href=""><img src="image2"></a>] would be my array Commented Jun 21, 2012 at 6:21
  • That array is not valid JS syntax. Please show the code that creates the array, and the original source html. If you're trying to say that you have an array of references to DOM elements please say so more clearly (and, again, show your code). Commented Jun 21, 2012 at 6:24
  • this is the DOM reference, and you are trying to use parent() witch is a jQuery method, you need to wrap this with 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. Commented Jun 21, 2012 at 6:31

5 Answers 5

2

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');
});

Example


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);
});

Example

answered Jun 21, 2012 at 6:30
Sign up to request clarification or add additional context in comments.

3 Comments

I cant do that , this is being returned to me , is not like I am going trough markup and getting the values , all I can work with is [<a href=""><img src="image1"></a>,<a href=""><img src="image2"></a>]
Yes you can, here's a working fiddle: jsfiddle.net/ZZVXf/7 Be careful with your syntax.
If 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) {}).
1

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.

answered Jun 21, 2012 at 6:34

3 Comments

that is exactly what we tried , see fiddle please, this is log Object #<HTMLImageElement> has no method 'parent' from your code and my array , note that I am getting this returned trough imagesLoaded jquery plugin .
@Benn - this answer is not the same as your fiddle, this corrects the problems in your fiddle and it works: jsfiddle.net/ZZVXf/8
I think you misunderstand what $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.
0
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ドル'));
});
answered Jun 21, 2012 at 6:27

2 Comments

thnx for tying but why use regex when this should be a normal foreach? here is fiddle , is strange jsfiddle.net/ZZVXf/6
@Benn i've not understend your question, sorry. I think that you have an array and you want to search src value and put it in some href. I'm sorry.
0

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

answered Jun 21, 2012 at 6:37

Comments

0

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).

answered Jun 21, 2012 at 6:38

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.