0

Trying to export an array of objects in Javascript ES6:

settings.js

const elements = [
  {
    input: document.getElementById("name"),
    required: true,
    min: 1,
    max: 100,
    type: "alpha"
  },
  {
    input: document.getElementById("addr1"),
    required: true,
    min: 1,
    max: 100,
    type: "alphanumeric"
  }
];

export default elements;

validation.js

import elements from "./settings.js";

I always get the error:

Uncaught SyntaxError: Unexpected identifier

index.html

<!DOCTYPE html>

<div id="form">
    <form action="">
        <label for="name">Name</label>
        <input type="text" id="name" name="name" placeholder="Your name..">
        <div class="error"></div>

        <label for="addr">Address 1</label>
        <input type="text" id="addr1" name="addr1" placeholder="Your first address..">
        <div class="error"></div>

        <label for="addr1">Address 2</label>
        <input type="text" id="addr2" name="addr2" placeholder="Your second address..">
        <div class="error"></div>

        <label for="city">City</label>
        <input type="text" id="city" name="city" placeholder="Your city..">
        <div class="error"></div>

        <label for="state">State</label>
        <select id="state" name="state">
            <option value="australia">Australia</option>
            <option value="usa">USA</option>
        </select>
        <div class="error"></div>

        <label for="zip">Zip Code</label>
        <input type="text" id="zip" name="zip" placeholder="Your zip code..">
        <div class="error"></div>

        <input type="button" id="confirm" value="Ok">
        <input type="button" id="cancel" value="Cancel">
    </form>
</div>

<script src="./validation.js"></script>
</body>

</html>

Cannot figure out the reason!

13
  • Are you using babel? Commented Aug 29, 2018 at 17:43
  • no @ArupRakshit . . . Commented Aug 29, 2018 at 17:44
  • your import depends on the page element? I'm not sure you can guarantee that your page elements are loaded (and have the right context) to be able to do the document.getElementById() there. Do you get the problem if you change that to a literal string or a number? Commented Aug 29, 2018 at 17:44
  • 2
    How are you loading the JS file? Please include the full error with location information if possible. Commented Aug 29, 2018 at 17:50
  • 2
    I don't see that error here stackblitz.com/edit/js-uvre2b?embed=1&file=index.js The values for name and addr1 are not properly set though. Commented Aug 29, 2018 at 17:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.