0

I have jquery UI and jQuery linked on my page, and I have the following jQuery UI code currently on the page:

  $(function() {
    $( "#draggable" ).draggable({ containment: "parent"});
    });

I want to have this jquery code run below the code above:

function new() {  
    $("p").append("<strong>Hello</strong>");
}

But for some reason it's not working when both are in the same place and as a newbie to jQuery, I'm not sure what's wrong (both of the codes were mostly copied and pasted from the site). I suspect it's something to do with the function statement.

2
  • 1
    What exactly do you mean when you say "it's not working"? What do you expect to happen? Commented Dec 2, 2012 at 16:54
  • @brianpeiris I expect both parts of the code to run Commented Dec 2, 2012 at 17:06

1 Answer 1

3

new is a reserved keyword in javascript (it's used as syntax to create new objects) so you can't use it as a function name. Try using a different name for your function, for example:

function mynew() {  
    $("p").append("<strong>Hello</strong>");
}
Sign up to request clarification or add additional context in comments.

8 Comments

By the way, you now have enough reputation to give upvotes, so conside also upvoting my answer (up arrow) if you are happy with it :-)
Bonus question: Why does appending this prevent the .draggable function from working: $("body").append("<div id="draggable" class="ui-widget-content"> <br> <textarea id="contents" cols="20" rows="10"></textarea> </div>");
@lawm You need to initialize the draggable after you append it to the document.
Maybe because it doesn't have a parent div to be used for your 'containement' parameter? not sure.. you better create a jsfiddle.net that demonstrate your problem so it's easier for people to help you..
You were not escaping your html double quotes inside your append function, I used just single quotes to make it work, see a working version of your fiddle jsfiddle.net/MJKhq/3
|

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.