0

I have a javascript popUp function...

popUp(title, body){
    $('body').append('<div id="popupWindow"><div class="close"></div>
        <h2 class="title">'+title+'</h2>
        <div class="body">'+body+'</div>
        </div>");
    // then on $('.close').click(func(){ remove() etc... } );
    // and keyCode == 13 remove();
}

that inserts the two arguments into popup window that is then inserted into the html of the document. I had the idea of having a <form> as body but it craps out??

I have tried various things and it would appear to be the html as the argument which is causing the problem. I have limited javascript/jQuery experience, so any help you guys/gals can give would be much appreciated.

Thanks, Si

It's not a complete copy - However your right, there was a stray quote i hadnt escaped in the form thanks guys (hour well spent for a typo...)

4
  • 3
    Possibly a typo, but the end of your html string is using the wrong type of quote. Commented Aug 8, 2012 at 14:05
  • 2
    And you can't have new lines in JS strings Commented Aug 8, 2012 at 14:06
  • Any reason you can't use the jQuery UI Dialog plugin? Commented Aug 8, 2012 at 14:06
  • 1
    @Quentin you can, if you escape them with \ Commented Aug 8, 2012 at 14:08

3 Answers 3

1

Set an identifier for your form and use it in JavaScript code.

<form id="myform"></form>


popUp(title, body){
    $('#myform').append('<div id="popupWindow"><div class="close"></div>
        <h2 class="title">'+title+'</h2>
        <div class="body">'+body+'</div>
        </div>');
    // then on $('.close').click(func(){ remove() etc... } );
    // and keyCode == 13 remove();
}

By the way, there was an error with quotes in your append statement.

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

Comments

1

Is popUp a function? You also have some line-break issues. Personally, I would use jQuery to construct your elements... it's much more elegant.

function popUp(title, body){

    var $popup = $('<div />').attr('id', 'popupWindow');
    var $close = $('<div />').addClass('close');
    var $title = $('<h2 />').addClass('title').text(title);
    var $body = $('<div />').addClass('body').html(body);

    $popup.append($close).append($title).append($body);
    $('body').append($popup);
    // To append to a form, you can target it here instead of 'body'

}

popUp('My Title', 'Some <strong>body</strong> content');

Comments

0

I think the problem using single and double quotes in append method, use ASCII, codes like :

 + String.fromCharCode(34) + title + String.fromCharCode(34) + 

String.fromCharCode(34) is used for " and String.fromCharCode(39) is used for '.

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.