Googie Shakarane
Googie Shakarane

Reputation: 47

How to fix 'missing) after argument list' error in Javascript

Why is this giving me this error?

<script>
    function loadImg() {
        var imageChosen = document.getElementById('imageChosen').value.replace("C:\fakepath\","");
        alert(imageChosen);
        document.getElementById('image').src = imageChosen;
    }
</script>

I expect the image with id "image" to show the chosen image.

Upvotes: 0

Views: 69

Answers (2)

The problem is due to the escape string character \ (backslash)

When using strings in Javascript we may escape some character in the string. For example a break line (\n) or even a "(double quotes) when declaring the string or even an backslash \ need a escape.

Examples:

x = "my \\" // Will output as the same as "my \"

z = "my \"quotes\" // Will output as 'my "quotes" '

Upvotes: 0

Bryan
Bryan

Reputation: 1005

The value in your call to replace() is not escaped properly.
The value should instead be:
"C:\\fakepath\\",""

Read more about escaping strings here

Upvotes: 5

Related Questions