Variable inside function

3 views
Skip to first unread message

Tom

unread,
Aug 31, 2010, 11:03:42 PM8/31/10
to mozilla-labs-jetpack
Hello, good evening! I am with the following question ... Below is a
snippet of code:

What I need is to know how do I put the variable name "nome_procura"
within the search function.

I tried to put "+", "." .... and nothing works. If I leave the code as
it is, the function will look inside the tag "ALT" everything that has
name "nome_procura" and actually I wanted to appear "Mastercard",
where he came from a variable.

procura_string = 0;
$ (Doc). Find ('img'). Each (function () (
nome_procura var = "Mastercard";
procura_string = $ (this). attr ('alt'.) search (/ nome_procura / i);

Thanks,
Tom

cc

unread,
Sep 1, 2010, 1:20:24 AM9/1/10
to mozilla-la...@googlegroups.com
On 2010-08-31 20:03, Tom wrote:
Hello, good evening! I am with the following question ... Below is a
snippet of code:

What I need is to know how do I put the variable name "nome_procura"
within the search function.

I tried to put "+", "." .... and nothing works. If I leave the code as
it is, the function will look inside the tag "ALT" everything that has
name "nome_procura" and actually I wanted to appear "Mastercard",
where he came from a variable.

procura_string = 0;

Not sure what this line is for.


$ (Doc). Find ('img'). Each (function () (
Not
    $(document).find('img').each(function () {
?
nome_procura var = "Mastercard";

This line should be more like
    var nome_procura = "Mastercard";
(see syntax for var statement)


procura_string = $ (this). attr ('alt'.) search (/ nome_procura / i);

I don't really know jQuery, so I can't tell exactly what $(this) does (perhaps returns the jQuery-enhanced object for the `this` pointer?), but the rest of it can be suitably rearranged, perhaps as follows:
    var procura_string = $(this).attr('alt').search(new RegExp(nome_procura, "i"));
See the documentation on RegExp for some background, especially regarding the /pattern/ syntax, which treats whatever is inside the slashes as a regex character string (which is why it didn't access the variable's contents, but only used its name).

});
(Presumably you end the statement this way, although you didn't post this....)

-- 
cc | pseudonymous | <http://carlclark.mp/>

Wes Kocher

unread,
Sep 1, 2010, 1:29:49 AM9/1/10
to mozilla-la...@googlegroups.com
I think you have to use the RegExp() function:
http://bytes.com/topic/javascript/answers/94340-variable-inside-regex

Thanks,
Tom

--
You received this message because you are subscribed to the Google Groups
"mozilla-labs-jetpack" group.
To post to this group, send email to mozilla-la...@googlegroups.com.
To unsubscribe from this group, send email to
mozilla-labs-jet...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mozilla-labs-jetpack?hl=en.


Tom

unread,
Sep 1, 2010, 9:14:45 AM9/1/10
to mozilla-labs-jetpack
Hello ladies!

Thanks for helping me once again, it worked! I used the line:
procura_string = $ (this). attr ('alt'.) search (new RegExp
(nome_procura, "i"));

And just in case "nome_procura" is a variable that comes from an
external function.

Topic Done!

Thanks,
Regards,
Tom.
Reply all
Reply to author
Forward
0 new messages