Solved

JQuery code to move one element before another

Posted on 2013-11-26
9
439 Views
Last Modified: 2013-11-26
Hi!

I'm VERY new to jquery, so please bear with me.

I need to move an element in my wordpress blog. The element is created by a plugin I use (called mailpoet), and the plugin author told me to use jquery to move it.

Here's the jquery I have:
$('p.form-submit').each(function() {
    $(this).insertAfter($(this).parent().find('p.wysija-after-comment'));
});

Open in new window


I also enqueued the .js file (and can see it in the page source), but for some reason, the element is not getting moved. I have no clue how to debug this to find the problem.

Here's the page: http://dev.goinspire.com/men-demand-equal-rights-jwrp/

The element to be moved is the "Post Comment" button, it should be after the checkbox where they can click to subscribe.

If anyone can help me I'd REALLY appreciate it.
0
Comment
Question by:etech0
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
9 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 39678466
Try this :
jQuery('p.form-submit').insertAfter('p.wysija-after-comment');

Open in new window

If it don't work this :
jQuery(document).ready(function() {
        jQuery('p.form-submit').insertAfter('p.wysija-after-comment');
});

Open in new window

If it don't work this :
jQuery(window).load(function() {
        jQuery('p.form-submit').insertAfter('p.wysija-after-comment');
});

Open in new window

like this
0
 
LVL 10

Author Comment

by:etech0
ID: 39678483
Thanks for your reply!

For some odd reason, none of them are working.

Note: I'm putting the jquery code into a file called submitbutton.js, and i'm enqueueing it via this:

wp_enqueue_script( 'submitbutton', get_template_directory_uri() . '/js/submitbutton.js', array( 'jquery' ), '',  true );

Open in new window

0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39678571
Your page is showing a 404 error (file not found) for this file:

http://dev.goinspire.com/wp-content/themes/goinspire/js/submitbutton.js

Check it exists and is in the right place.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39678584
Once you've got your files sorted, this is the jQuery you'll need:

jQuery('#commentform .form-submit').appendTo('#commentform');

Open in new window

0
 
LVL 10

Author Comment

by:etech0
ID: 39678587
That's where it is.

Except that dev.goinspire.com points to goinspire.com/dev, so the file is really at goinspire.com/dev/wp-content-themes/goinspire/js/submitbutton.js

Do you think that would make a difference?
0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39678606
That's where it is.
Sorry to dissappoint, but no it isn't. Open this in your browser:

http://dev.goinspire.com/wp-content/themes/goinspire/js/submitbutton.js

You will get a File Not Found error!

Whereas this works:

http://dev.goinspire.com/wp-content/themes/goinspire/js/smoothscroll.js

So the issue isn't one of mapping
0
 
LVL 10

Author Comment

by:etech0
ID: 39678614
You're right! So strange, because I'm using Sublime SFTP and it said the file was there, but when I looked in FileZilla I didn't see it, so I uploaded it from there.

Now the button is in the right place. Thanks!

Question: I'm using the third code option you gave. Any reason to try the others? (speed or something?)

Just curious...
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39678655
It was leakim that provided the first set of examples, not me, but fundamentally all three of his suggestions are the same.

When you use any jQuery that operates on the DOM it's common practice to make sure the DOM is ready before firing the function - the standard way to do that is to use the document ready block - the second example.

As you're putting the code in it's own file, then I would suggest using the document ready method.

For the actual function that moves the button - doesn't really matter whether you use my method (append) or leakims (insertAfter) - just wrap either in jQuery(document).ready()
0
 
LVL 10

Author Closing Comment

by:etech0
ID: 39679067
Got it. Tremendous thanks to both of you for such clear, quick explanations!
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question