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
a source to share
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 :)
a source to share