0

I have created a function createFileSelect() which creates elements:

function createFileSelect() {

    var fSpan = $("<span/>",
        {
            "id" : "fileUpload-span",
            "class" : "btn btn-add",
            "text" : "Add a file.."
        }); 

    var fInput = $("<input/>", {
        "type": "file",
        "name": "files[]",
        "id": "fileUpload"       
    });

    return $(fSpan + fInput);
}

Then I call this using:

$("#fileUpload-holder").append(createFileSelect());

However, I am getting an error

Syntax error, unrecognized expression: [object Object][object Object]

How can I get this working with the (2) elements into the dom using a function?

Thanks

3 Answers 3

2

Change the return statement to use jQuery's .add method:

return fSpan.add(fInput);

If you want the input within the span, use this instead:

return fSpan.append(fInput);
Sign up to request clarification or add additional context in comments.

Comments

0

Replace your following line:

return $(fSpan + fInput);

for this one:

return fSpan.add(fInput);

Comments

0

The + operator is not defined for objects in javascript, so they are coerced to strings first (which is the "[object Object][object Object]" string). Then, as you pass the string to the $ function, it tries to interpret it as HTML, which in turn generates the syntax error you are seeing.

To merge two jQuery objects together, you can use the .add() function, that returns a new object that contains both:

return fSpan.add(fInput);

Comments

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.