0

I have the following JavaScript object:

function Project(name)
{
    this.panel = $('#worksheet_panel');
    this.name = name;
    this.workBox =
        '<div class="worksheet_box">'+
        '    <div class="list-group">'+
        '        <button class="btn btn-block btn-primary btn_work_add" onclick="setProject('+this.name+');" data-toggle="modal" data-target=".workModal">Add</button>'+
        '    </div>'+
        '</div>'+
        '</div>';
    function changeName(name)
    {
        this.name = name;
    }

    this.addElement = function()
    {
        this.element =
            '<div id="1" class="col-md-3 table-bordered worksheet_overall_box" draggable="true" ondragstart="drag(event)">'+
                '<div class="table-bordered title_box">'+
                '<h4 class="title text-center worksheet_title">'+name+'</h4>'+
                '</div>'+
        this.workBox;
        this.panel.append(this.element);
    }
}

Now as you can see the this.workbox has an onclick function that calls this function:

function setProject(name) {
   selected_project = projects[name];
}

Now when ever I click the button I get the following error:

ReferenceError: asasa is not defined

setProject(asasa);
1
  • I don't see you actually naming the workbox... Could that be the problem? Though I don't see any mention of "asasa" in your code, that's weird... Commented Feb 24, 2014 at 0:09

1 Answer 1

1

Have a look at the generated code. It will be something like:

onclick="setProject(asasa);"

which means you are trying to pass the variable asasa to the function. Such a variable does not exist.

It looks like you want to pass a string containing that value, so you have to add quotation marks:

' ... onclick="setProject(\''+this.name+'\');" ...'

so that the generated code is

onclick="setProject('asasa');"
Sign up to request clarification or add additional context in comments.

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.