-1

I don't why my website doesn't go through the if statement. The objective is to display an image when I click a button, but it gets stuck. If anyone could help I would be very happy :D

function showllum() {
        $.get('llum.txt', function(data) {
            alert(data);
            if (data === "Llum") {
                    alert("1");
                    $('#showdada').empty();
                    $('#showdada').prepend($('<img>', {
                            id: 'llumpng',
                            src: 'llum.png'
                        }

                    ))
            } else if (data === "Molta Llum") {
                    alert("2");
                    $('#showdada').empty();
                    $('#showdada').prepend($('<img>', {
                            id: 'moltallumpng',
                            src: 'molta llum.png'
                        }

                    ))

                } else if (data === "Poca Llum") {
                        alert("3");
                        $('#showdada').empty();
                        $('#showdada').prepend($('<img>', {
                                id: 'pocallumpng',
                                src: 'poca llum.png'
                            }

                        ))

                    } else if (data === "Fosques") {
                            alert("4");
                            $('#showdada').empty();
                            $('#showdada').prepend($('<img>', {
                                    id: 'fosquespng',
                                    src: 'fosques.png'
                                }

                            ))

                        }})}
1
  • Comments are not for extended discussion; this conversation has been moved to chat. Commented Jan 1, 2019 at 2:19

2 Answers 2

1

You need to add a dataType - http://api.jquery.com/jQuery.ajax/

$(document).ready(function() {
    $.ajax({
        url : "helloworld.txt",
        dataType: "text",
        success : function (data) {
            if(data === '*'){...}
        }
    });
}); 

But even if you change to this, you won't get results from a localdrive, you'll need an actual http server to serve the static textfile. Please check the network requests and update here what response you get.

Sign up to request clarification or add additional context in comments.

2 Comments

$.get doesn't need a dataType - test with $.get('https://www.w3.org/TR/PNG/iso_8859-1.txt', data => console.log(data))
My bad, thought he used the ajax call.
0

In addition to the carriage return in the string that was causing the original problem (which is fixed with data.trim()), your code can be simplified since there's a pattern to your logic - each case clears the showdada element and then prepends an image using the pattern src = `${data.trim()}.png` in lowercase and then the id removes all spaces and periods from the src:

function showllum() {
  $.get('llum.txt', function(data) {
    $('#showdada').empty();
    const src = `${data.trim()}.png`.toLowerCase();
    $('#showdada').prepend($('<img>', {
      id: src.replace(/[ .]/g,''),
      src
    })
  });
}

7 Comments

1.js:16 Uncaught TypeError: data.trim(...).toLoweeCase is not a function at Object.success (1.js:16) at u (jquery-3.3.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-3.3.1.min.js:2) at k (jquery-3.3.1.min.js:2) at XMLHttpRequest.<anonymous> (jquery-3.3.1.min.js:2)
that's a typo I fixed - toLoweeCase -> toLowerCase
also make sure the /g is in the regex - I forgot it in the original post
Man, literally, you saved my final course project <3, if there's any way I could give you reputation or some kinda thing tell me :D.
It may sound exaggerated, but when you spend your hours with an error that is quite impossible for you to resolve and someone helps you, seriously <3
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.