0

For a project I need to make a sort of constructor to change some element in a page:

I have for example:

var element = ".h-event img";
var match   = "eq(0)";
var type    = "img";
var value   = "breakfast.png";

Allowing me to construct:

$(element).attr('src', value);

With this code:

if(type=='img') {
    $(element).attr('src', value);
}

The problem I have actually, is to use the eq() selector in jQuery by using by match variable.

How can I do that ?

This is what I already tried without success:

var match = (match=='') ? '': '.'+match+'.';
if(type=='img') {
    $(element)match.attr('src', item.value);
}

1 Answer 1

2

You can rather use :eq selector for better concatenation:

var match = (match=='') ? '': ':'+match+'';
if(type=='img') {
  $(element+match).attr('src', item.value);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Good solution. But if i use Zepto, the :eq isn't available. Do you have another solution to support Zepto by using eq() please ? zeptojs.com/#eq
try using ` $(element)["eq(0)"]().attr('src', item.value);`
I can accept it for jQuery but if I would like to use next, find(), ... it will not work.
why not?? zeptojs do not supports it??

Your Answer

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.