/    Sign up×
Community /Pin to ProfileBookmark

Begins with statement

I have the following statement:

[CODE]fullID = this.id;
alert(fullID);
if(fullID^=’moveUpMod’){
direction = ‘+’;
alert(‘+’);
} else {
direction = ‘-‘;
alert(‘-‘);
}[/CODE]

When I click on a link for up arrow, I get a “moveModUp3”. But for the direction, I get a ‘-‘. For a down arrow, I also get a ‘-‘. Why when I click on ‘moveUpMod’ links, why does it think I want to move it down?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@deathshadowAug 03.2014 — This might be a silly question, but why are you doing a binary XOR equals, then expecting a string result?!? that doesn't make any sense. You can't "exclusive or" a string.

Did you mean to do a == instead?

Also not sure what this has to do with jQuery... since what you have there is vanilla js. I'd probably also not waste time with the variable for nothing as an object property usually isn't slow enough to warrant it -- unlike say, a numbered array index.
Copy linkTweet thisAlerts:
@droidusauthorAug 03.2014 — OK- so I am pretty new to this. How would you recommend that I solve this then?
Copy linkTweet thisAlerts:
@deathshadowAug 03.2014 — alert(this.id);
if (this.id == 'moveUpMod') {
direction = '+';
alert('+');
} else {
direction = '-';
alert('-');
}


== instead of ^=

since ^ means binary XOR... I'm not sure why you had that ^ in there in the first place.

Though if you had more values to check, I'd lose the IF and use a SWITCH instead. For example:

alert(this.id);
switch (this.id) {
case 'moveUpMod':
direction = '+';
alert('+');
break;
case 'moveDownMod':
direction = '-';
alert('-');
break;
default:
alert('unknown ID' + this.id);
}
Copy linkTweet thisAlerts:
@droidusauthorAug 04.2014 — alert(this.id);
if (this.id == 'moveUpMod') {
direction = '+';
alert('+');
} else {
direction = '-';
alert('-');
}


== instead of ^=

since ^ means binary XOR... I'm not sure why you had that ^ in there in the first place.

Though if you had more values to check, I'd lose the IF and use a SWITCH instead. For example:

alert(this.id);
switch (this.id) {
case 'moveUpMod':
direction = '+';
alert('+');
break;
case 'moveDownMod':
direction = '-';
alert('-');
break;
default:
alert('unknown ID' + this.id);
}
[/QUOTE]


I used ^= because it means begins with. will it still work with ==?
Copy linkTweet thisAlerts:
@deathshadowAug 04.2014 — ^ does not mean 'begins with' in JavaScript. That's Regular Expressions. In JS/C/PHP/JAVA/every other C syntax language it's a binary XOR, something ENTIRELY different.

So what you're saying is you want to perform a String.match? Actually, indexOf without a regex would probably be faster.

if (this.id.indexOf('moveUpMod') == 0) {

though if you want to use the regex:

if (this.id.search(/^moveUpMod/) !== -1) {

Though why you'd have an id complex enough for that type of operation I'm not sure. Just what does the rest of that ID look like?

For reference:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Bitwise_operators

Bitwise operator that's an XOR

You're thinking regular expressions:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

Which means you have to use a String.[i]method[/i] that accepts them like search, match, replace...
Copy linkTweet thisAlerts:
@droidusauthorAug 07.2014 — I was looking at this: http://api.jquery.com/attribute-starts-with-selector/.

And I have different modules. There are two options: move it up/down. The span id of the container holding the up arrow is moveUpMod1 for module 1 (there's a module 2, 3, 4, etc.). Then there is a span of id moveDownMod1, and so on. Is there a better way of doing this?
Copy linkTweet thisAlerts:
@deathshadowAug 07.2014 — Those are jQuery selectors, and only work with jQuery methods, not JavaScript ones... which is just another of their pointless cryptic painfully inconsistent bits of gibberish that make my advice be pitch jQuery in the trash. It has prevented you from learning the underlying language and confused you with a construct exclusive to it's actions that have NOTHING to do with how JavaScript does things.

Without seeing your markup or where this routine is being called from, it's hard to weigh in more on what you are doing -- it sounds like you are trying to call the same routine from different user actions. If so you'd probably be better off making them be separate actions/events.
×

Success!

Help @droidus 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 5.30,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...