How can I add an id to a paragraph element in a .before function?

How can I add an id to the P element in this bit of jQuery?

$( ".inner" ).before( "<p>Test</p>" );


Why make it complicated ?

$(".inner").before("<p id='myid'>Test</p>");


$("#a").before($("<p>Test</p>").attr('id', 'myid'));

You can with this:

$(".inner").before("<p id='myId'>Test</p>");

This is another way

$(".inner").before($("<p>Test</p>", {id: "myId"}));


$(".inner").before($("<p>Test</p>").attr(id, "myId"));

Just to list a few.


There are many ways to do it:

var p = $( "<p />" ).text( 'Test' ).attr( 'id', 'identifier' );
$( ".inner" ).before( p );

Or slightly more simply:

$( "<p />" ).text( 'Test' )
            .attr( 'id', 'identifier' )
            .insertBefore( '.inner' );

Or you can just add it to the HTML string used to create the paragraph element:

$( ".inner" ).before( "<p id='identifier'>Test</p>" );

Need Your Help

Help with running crontab from root

macos cron root cronjob

I'm using Mac OS X and having trouble getting a cron job to run. I type the following:

Rails 3 - no such file to load -- openssl

ruby-on-rails ruby ruby-on-rails-3 openssl rvm

when running a Rails server, I get the following error: