/    Sign up×
Community /Pin to ProfileBookmark

bObj button onclick

Another Weerd One. I got an insert row function running with bObj but wanted to add some flexibility to the function and decided to add some if statements to it but can’t figure out why it isn’t working as exspected.

[B][U]SCRIPT[/U][/B]

[CODE]
function addtoinvoice(){
var bObj=document.getElementsByClassName(‘button’);
for(var i=0; i< bObj.length; i++)
{
if(bObj[i].innerText==’Buy More!’) //added if to test button change?
{
bObj[i].onclick = function()
{
//alert(‘Yeah!’);
alerted();//should work!!
return;//to avoid rest of script
}
if(bObj[i].innerText===’Buy Now!’)
{
bObj[i].onclick = function()
{
//alert(bObj[0].innerText);
additem(this);
}
}
};
}

function additem(button)//oh that’s it have to reference button??
{
alert(this.innerText);
}
//onclick doesn’t run at all??

function alerted(button)//refferenced button
{
alert(this.innerText);
}
//this should work?
[/CODE]

Any advice greatly appreciated.:p

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@THEFOOLauthorApr 26.2012 — This only runs the first function which is the important one, but I'd like to run the second alerted() function because the innerText of the buttons are changed and I don't wan't to change their classNames even though that maybe the way to go?

[B][U]SCRIPT[/U][/B]
[CODE]
function addtoinvoice(){
var bObj=document.getElementsByClassName('button');
for(var i=0; i< bObj.length; i++)
{
if(bObj[i].innerText==='Buy Now!') //RUNS!!
{
bObj[i].onclick = function()
{
//alert(bObj[0].innerText);
additem(this);
return;
}
if(bObj[i].innerText==='Buy More!')//DOESN'T
{
bObj[i].onclick = function()
{
alerted();//OOPS left out (this)
return;
}
}
};
}


}[/CODE]


Left out this in alerted()

No! not even adding the this solved it. Anybody explain why?
×

Success!

Help @THEFOOL 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,
)...