form {
display: block;
border: 1px dotted #222;
text-align: center;
margin: 20px 20px;
padding: 20px 20px
}
a {
}
JavaScript
/**
* jQuery seems to have problems selecting elements in a form when
* there is an (input) element with ID="nodeType" in that form
*
* Test 1:
* create a form with an input element with an id not equal to "nodeType"
* in the form submit, count the buttons in the form in 2 ways
* first via jQuery, then via native selecting method
*
* Test 2:
* create a form with an input element with an id "nodeType"
* in the form submit, count the buttons in the form in 2 ways
* first via jQuery, then via native selecting method
*
* Expected result, in both cases the count for native and jQuery
* should be 1.
*
* Result from test 1 is twice 1 -> ok
* Result from test 2 is 1 for native, 0 for jquery -> not ok
*/
$('form').live('submit', function (evt) {
evt.preventDefault();
var form = $(this),
buttonsJQuery = form.find('button'),
buttonsNative = document.getElementById(form.attr('id'))
.getElementsByTagName('button');
alert('jQuery found ' + buttonsJQuery.length + ' buttons' + "\n"
+ 'Native found ' + buttonsNative.length + ' buttons');
});
/**
* Just some more testing, check if I can select the button(s)
*/
$(document).ready(function () {
console.log($('button').length === 2); // true
console.log($('#node-type').length === 1); // true
console.log($('#nodeType').length === 1); // true
$('.test') // and this fails again:
.show()
.click(function (evt) {
evt.preventDefault();
var form = $(this).parent(),
buttonsJQuery = form.find('button'),
buttonsNative = document.getElementById(form.attr('id'))
.getElementsByTagName('button');
alert('jQuery found ' + buttonsJQuery.length + ' buttons' + "\n"
+ 'Native found ' + buttonsNative.length + ' buttons');
});
});
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.