Search submit button in all forms with textboxes with jquery

I'm working on a Firefox plugin that looks for a webpage for all textareas and puts an alert before the submit button.

my code looks like this

var submitWarning = content.document.createElement("div");
submitWarning.innerHTML = "Fancy Message";
$('textarea', window.content.document).each(function() {
    var form = $(this, window.content.document).parents('form:first');
    $(form, window.content.document).children('input[type=submit]').each(function() {
         form.insertBefore(submitWarning, this);
    });
});

      

If I search all posts with $('input[type=submit]'.each

, it works great, but since I added the thing with the textbox and form:first

I am having problems (nothing happens)

ps i am using window.content.document thingy because this is an ff plugin and it won't work without it

+2


a source to share


3 answers


You need to change it a little, for example:

var submitWarning = content.document.createElement("div");
submitWarning.innerHTML = "Fancy Message";
$('textarea', window.content.document)
   .closest('form')
   .find('input[type=submit]')
   .before(submitWarning);

      



Argument syntax $(selector, context)

, when searching for a form first .closest()

, which makes it easier, also when you have an element, you can just use $(this)

inside it .each()

, there is no need to search for it again. Alternatively, you can use .before()

to simplify :)

+1


a source


: has () selector is the best choice for me. Get rid of your extended code and use it instead.



var buttons = $("form:has(textarea) input[type=submit]", window.content.document);
$("<div>").html("Fancy Message").insertBefore(buttons);

      

+1


a source


Try it var form = $(this, window.content.document).closest('form');

. Not sure if this is a ticket, but this is the first thing on my part.

0


a source







All Articles