/    Sign up×
Community /Pin to ProfileBookmark

How to start a jquery function on an object?

Hi,

I need to make this static jquery code totally dynamic.
Question is, how to start the abc function on the newimage object?

The Problem is this line:
[B] $(newimg).abc({[/B]

OLD static, working:

[CODE]
<img src=”001.jpg” class=”dosomething” />

$(document).ready(function(){
$(“img.dosomething”).abc({
option1: A,
option2: B
});
});[/CODE]

NEW dynamic:
(values hard-coded for testing only)

[CODE]
$(document).ready(function(){
var newimg= document.createElement(‘img’);
newimg.src = “001.jpg”;
newimg.setAttribute(‘class’, ‘dosomething’);

$(newimg).abc({
option1: A,
option2: B
});
});[/CODE]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Jeff_MottJan 28.2010 — The Problem is this line:

[B] $(newimg).abc({[/B][/QUOTE]


What's the problem with that line? Are you getting an error message for that line?
Copy linkTweet thisAlerts:
@WebnerdJan 28.2010 — You need to register it with jQuery:

<i>
</i>$.fn.abc = function(){
$(this).each(function(){

<i> </i> // your code here
<i> </i>});
}



as a valid jQuery object function.

Actually, where are "A" and B" defined?
Copy linkTweet thisAlerts:
@WebnerdJan 28.2010 — You could also do this:

<i>
</i>
<i> </i>$('&lt;img&gt;').addClass('dosomething').attr('src','somesrc').abc({
<i> </i> option1: A,
<i> </i> option2: B
<i> </i> });



And lose this code entirely:
<i>
</i> var newimg= document.createElement('img');
newimg.src = "001.jpg";
newimg.setAttribute('class', 'dosomething');

×

Success!

Help @janmartin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...