File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Problem with Javascript string comparison Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Problem with Javascript string comparison" Watch "Problem with Javascript string comparison" New topic
Author

Problem with Javascript string comparison

Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
Here is my code (abstracted slightly to remove sensitive information)



However this does not seem to work, i.e. 'Option1' is not output to the screen.
I have tried several variations, check the elementbyid directly in the if statement, I have used both '==' and '===' just to see.

Any suggestions?

Thanks greatly in advance,
NDG.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
I have it from a source that this works fine with several browsers....however I am running this locally (tomcat) in eclipse helios...does anyone have information on this internal browser, and why it wouldnt run only javascript 'if' statements?

Thanks,
ND.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Please UseCodeTags, and you haven't really explained what the issue is,


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Antony Kennedy
author
Greenhorn

Joined: May 10, 2011
Posts: 16

Hi.

Although some browsers allow you to access the current value of a select box directly, this is not the preferred way or most compatible to do it.

All browsers do expose a property called "selectedIndex" against the select box, and the select box itself is an array of objects with properties called "text" (the displayed value) and "value" (the actual value).

There are various bits of your code that are not ideal, but hopefully this example explains where you are going wrong.



Hope that helps.


Co-author of Pro CSS for High Traffic Websites (http://apress.com/book/view/9781430232889)
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Antony Kennedy,

Thanks for your reply. I never thought of using this method before. I had tried switch statement before but to no avail.
I trialled your code above, sorry I merged the method with what I have now...and unfortunately it does produce the desired output.

Im so confused now, I have spent near 24 hours now working on this!!1 A flippin' if statement!!!

Do you know anything about the internal browser? Or why it would allow if statements as long as you dont a value from an element? Why it works with all other methods and functions of javascript, but not an if from an element?

I have never seen such behaviour, and I have ran into nearly every wall you would expect to hit.

Thanks,
NDG.
Antony Kennedy
author
Greenhorn

Joined: May 10, 2011
Posts: 16

Which browser are you using? Post your code.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Antony Kennedy,

My code is above, and I am running it with the internal browser in eclipse as well as testing with firefox. Neither of these have the desired result.
As I have mentioned I got people to test it yesterday, and it works fine for them. My code is 100% correct, there is something else going on.

Therefore if you have any pointers for browser code relationships and interactions I would much appreciate it. There has to be a reason why my browser simply ignores the if statement when using an elements value.

Thanks,
N Dean G.
Antony Kennedy
author
Greenhorn

Joined: May 10, 2011
Posts: 16

It has nothing to do with your if statement.

This is wrong:



This is right:


Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
As I mentioned, after your last post. I tried this, and it did not work either.
This is why I am asking anybody out there about if they know anything about the internal browser in eclipse or why my firefox would possibly be ignoring these statements.

Thanks,
NDG.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

I highly doubt that Firefox is ignoring your code. There's something else at work. What browsers have you tried (not "heard from a source") where it actually does what you intend?

Post a SSCCE showing the problem.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
I have tried (and my source being my colleague beside me with a different machine dual booted by the way, not some randomer), the internal browser in eclipse helios, firefox and IE. My colleague tried the same with a different version of IE, both running my code itself and replicating my code.

No luck.
I have no idea where to start looking to be honest.

Thanks,
NDG.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

Bear Bibeault wrote:Post a SSCCE showing the problem.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
I cannot make the code above any more concise/short or to the point - its the minimal that I can give....unless or have a completely different definition of SSCCE than I do.

The problem is NOT the code now - its the browser or why my browser does not work with this code - I have had 3 testers now with this code and its work for all of them except me - so if any out there has any suggestions as to potential cause of one browser not running a section of code that the exact same browser can run please let me know.

I feel sick that I have spent 48 hours on an if statement and got nowhere...

Thanks,
NDG.
Antony Kennedy
author
Greenhorn

Joined: May 10, 2011
Posts: 16

Have you tried some basic debugging?



Otherwise, your output must be different from other people's. Use view source, and a diff tool to compare. Or you have javascript disabled? Or you are running locally instead of via a server?
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
I have tested with alerts, and they do work...I have used other if statements and they do work, it is only when I use if statements in this fashion they dont - comparing strings from an elements value - and I am running locally, not on a server.

So

function addAspect()
{
var a = "hello";
var b = "hello";
if(a == b)
alert("hello");
}

works fine. I do not have javascript disabled as it works for other things.

Thanks,
NDG.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
Hi,

I also tried importing my project into another (clean) machine, and this if statement still does not perform!!! I am so confused now....

Any body have any idea? I have hit every possible problem in the world of computing, and I have never seen something like this...

Thanks greatly, as my head is exploding,
N D G.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

I will only ask one more time, please post a SSCCE showing the issue with no other fluff.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Bear Bibeault

As I mentioned, I cannot make it any more simple - the code above is the SSCCE - my short, self contained, correct example of what I am intending to be doing.

I'm sorry if we have a different definition, however I have reduced that code as much as possible.

What are you implying by SSCCE? And how I am suppose to reduce an already reduced piece of code? If I take anything out it wont be a SSCCE.

Thanks,
N D G.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

So you're saying that is the complete code? Then since there is nothing to call that function, the alert will never happen.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Paul Clapham

There is, if you look at the 'onclick' in the button in the <form>....and there is no definition in a SSCCE to be complete

Thanks,
NDG.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Have you debugged and see if

* Is the function being called? Add an alert right inside the function.
* What value is being returned with var aspect = aspectResult[aspectResult.selectedIndex].value; ? Alert the value!
* Is it going into the if statement? Add an alert
* Is the string being set?
* Is it finding the DOM element?

Eric
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Here's the last code you posted:


It doesn't have any onclick in it.

And you're right about SSCCE -- the bit I was thinking about was "self-contained". However that isn't self-contained.

I didn't look at the code you originally posted because there has already been discussion about it and you said you had changed it. So we're still waiting for the SSCCE.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Eric,

* Is the function being called? Add an alert right inside the function.

It is yes. The alert works. Not after the if statement though.

* What value is being returned with var aspect = aspectResult[aspectResult.selectedIndex].value; ? Alert the value!

The value is the string 'Option1', and its type is a string.

* Is it going into the if statement? Add an alert

No it is not. However if I change the if statement to be if ('hello' == 'hello') it works fine. It only happens when I am comparing my value from an element.

* Is the string being set?

It is yes.

* Is it finding the DOM element?

It is yes.

It just doesn't work altogether. The flow simply drops out, and exits the function.
Now you see why I am confused and frustrated beyond belief!!! Over 3 days now working at this goddamn if statement!!

Thanks,
NDG.
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Paul

I said I changed it yes, but that was attempting to try something different - but it didn't work either. So my code is the code you see at the very top (my first post). I apologise for the confusion, you have to remember I am beyond confused at the moment too

Thanks,
NDG.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What happens when you alert this before the if



Eric
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Eric

It alerts "Option1 == Option1 false"

'aspect' is a string and so is 'Option1', yet they dont compare when using a string comparison operator???

Thanks,
N D G.
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270



can you try this and make sure there are no spaces ? try to trim it if there are...


Time is what we want the most, but what we use the worst. -- William Penn
Nicholas Dean
Greenhorn

Joined: May 17, 2011
Posts: 23
@Robin John,

There are no spaces - the string is "Option1"....

Thanks,
NDG.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with Javascript string comparison