SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript else if not working

    Can anyone tell me why this else if statement is not working? I have displayed both on the page to check their values after being processed, and they are indeed the same. However, it doesn't work and I'm guessing it must be something with how I am comparing it. My code is below:

    Code JavaScript:
    <script id="template-download" type="text/x-tmpl">
    	function test(arg){
    		output = arg;
     
    		{% for (var i=0, file; file=o.files[i]; i++) { %}
    			<tr class="template-download fade">
    				{% if (file.error) { %}
    					<td></td>
    					<td class="name"><span>{%=file.name%}</span></td>
    					<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
    					<td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
     
              // This is where I am having problems. I specify index zero and sooner or
             // later file.name should match, but it never does and I'm not sure why??
            // So then I do else if ( "5.jpg" == file.name ) and it does work....As you can
           // see that I test it in the line right below the else if, and displays the same as file.name.
     
    				{% } else if ( output[0] == file.name ) { %}
    						<td class="name">{%=String(output[0])%}{%=String(file.name)%}</td>
    						<td class="preview">{% if (file.thumbnail_url) { %}
    							<a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
    						{% } %}</td>
    						<td class="name">
    							<a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
    						</td>
    						<td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
    						<td colspan="2"></td>
    					{% } %}
    						<td class="delete">
    							<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
    							<i class="icon-trash icon-white"></i>
    							<span>{%=locale.fileupload.destroy%}</span>
    							</button>
    							<input type="checkbox" name="delete" value="1">
    						</td>
    			</tr>
    			}
    		{% } %}
    	}
    </script>

    Any ideas? Thanks in advance!!!

  2. #2
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shouldn't that be..

    else if ( output[i] == file.name )

  3. #3
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I tried that first and it didn't work...then I tested it out on the line below it...was thinking maybe that code would work, but I was wrong. For some odd reason

    Code JavaScript:
    else if ( output[i] == file.name )

    will not work, but the following does..

    Code JavaScript:
    else if ( "5.jpg" == file.name )

    5.jpg5.jpg ===> is the output I get from
    Code JavaScript:
    <td class="name">{%=String(output[0])%}{%=String(file.name)%}</td>

  4. #4
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using

    output[i].localeCompare( file.name)

  5. #5
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, did that...now it runs but it displays all of them...even when I change output[i] to output[0] I should get only one that is true but I get all of them. Only one should match when I do it that way.

    So you know...output_string was a JSON array
    Code JavaScript:
    window.output = [];
    test(output_string); // output_string is a JSON array that I send to a test function and then I assign a global variable 
     
    function test(arg){
    		output = arg;
    }
    Maybe that is what is messing it up...

  6. #6
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tested and tested it...it is something to do with the JSON array...even though the value is the same it's not returning true. If that makes any sense.

    Here are the else if statements that works:
    Code JavaScript:
    else if ( output[0].localeCompare( output[0] ) ) { %} // LOL of course ;)
    else if ( file.name.localeCompare( "5.jpg" ) ) { %}

    These doesn't work
    Code JavaScript:
    else if ( output[0].localeCompare( file.name ) ) { %}
    else if ( output[0].localeCompare( "5.jpg" ) ) { %}

    Which doesn't make any sense, because I have printed out output[0] and the value is 5.jpg.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •