Can not set attributes in ie

I'm having trouble getting a script to add an onsubmit attribute to a form in IE. It works fine in Chrome though.

document.getElementsByName('form_name').item(0).setAttribute("onsubmit","add_to_form()");

It is grabbing the element fine and I can use getAttribute to print out pre-existing attributes just fine.

Answers


Your document probably puts IE in Quirks Mode, since in Standards Mode, the code seems to work OK.

To make the code work in Quirks Mode too, you can assign to the DOM property onsubmit rather than the HTML attribute. These two do not necessarily correspond to each other in old versions and Quirks Mode of IE. In your example, use this instead:

document.getElementsByName('form_name').item(0).onsubmit = add_to_form;

Let's rewrite this:

function listen(event, elem, func) { // cross-browser event listener
  if (elem.addEventListener) elem.addEventListener(event, func, false);
  else if (elem.attachEvent) elem.attachEvent('on' + event, func); // IE
}

listen('load', window, function() {
  var form = document.getElementsByName('form_name')[0];
  listen('submit', form, add_to_form);
});

Need Your Help

Access the underlying DirectX surface/rendertarget of a rendered WPF page/window

c++ wpf graphics directx unmanaged

I need to access the rendered raw pixels of a (managed) WPF page/window from unmanaged code.

How to select HTML Link element using xpath with multi criterias

html xpath

In a page HTML, there are several HTML links whose names contains some sub nodes.