Solved

How to get IE7 to accept a jQuery "name" attribute change on a <FORM>

Posted on 2008-06-13
9
6,515 Views
Last Modified: 2013-12-08
Sorry for the slightly hazy title.
I'm using jQuery and AJAX to load a page containing multiple DIVs with their own individual content, from various areas of our site. 2 of the DIVs I'm loading pull in some forms, however due to a legacy issue all the forms on the site have the same "name", which is "proDep".

I've used jQuery to change the name of the form...
    $('#iDerv form').attr("name", "dervForm");
and Firefox is happy with this and lets me address the 2 forms seperately, "proDep" and "dervForm", but IE just does something wierd that I don't understand.

It now treats "proDep" as a 2 dimensional array, with proDep[0] being "proDep", and proDep[1] being "newName".
I can access all the form fields using "proDep[0][4]" for example, but this is no good as I need to run functions that refer to fields in proDep as a 1d array.

It also then ignores the .append() ; function to add new elements to the form.

Is there a different way to change the name of an element or is this just something that IE can't handle?
// iDerv DIV hidden on load
$('#iDerv').hide();
 
...
...
 
function v3_processModel () {
         // looks at fields in first form
	var formMake = document.proDep.fjs_make.value;
	var formModel = document.proDep.fjs_model.value;
	
         ...
         ...
 
	// loads new content into DIV "iDerv", including another "proDep" form
	url = "pageHoldingproDep.cfm?make="+make;
	updateElem('iDerv',url);	
 
	// reveals the div and changes the form name
	$('#iDerv').show();
	$('#iDerv form').attr("name","dervForm");
	// add new buttons
	$('#iDerv form').append('<input type="submit" onClick="return v3_loadOptions();" value="Add Options">');
	$('#iDerv form').append('<input type="submit" onClick="return v3_processCalcClick();" value="Calculate">');
	return false;
}

Open in new window

0
Comment
Question by:comcar
[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
  • 5
  • 3
9 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 21779078
try:
$('#iDerv form:eq(0)').attr("name","dervForm");
0
 

Author Comment

by:comcar
ID: 21779557
Thanks hielo but that didn't do it I'm afraid. I've found out a few more things... although it's still got me stumped...

The form name does change, according to the IE Developer toolbar, it shows the name as "dervForm", but using DebugBar or Microsoft Script Debugger's console, if I type in;

"document.proDep[1].innerHTML" - get the HTML for the second form...
"document.dervForm.innerHTML" - and this throws a null object error!

I read somewhere about someone having similar sorts of troubles with ammending "onClick" values that IE would ignore, something to do with cloning? Anyone got any ideas?!
0
 
LVL 4

Expert Comment

by:FSIFM
ID: 21779643
If you defining the id name I dont think you have to specifiy that it is a form
$('#iDerv').attr("name","dervForm");

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:comcar
ID: 21779854
The ID is for the containing DIV, as shown in the simplified code snippet. The FORM is a child of the DIV, and I've also tried changing the code to:

$('#iDerv > form').attr("name","dervForm");
<div id="iDerv">
  <form name="proDep"></form>
</div>
 
// so with this line of code I'm trying to achieve the bottom example
$('#iDerv > form').attr("name","dervForm");
 
<div id="iDerv">
  <form name="dervForm"></form>
</div>

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 21780001
>>document.proDep[1].innerHTML" - get the HTML for the second form...
Correct. That's because you have two forms with the same name.
document.proDep[0].innerHTML

should give you the first form. As a matter of fact, you can even change the name via:
document.forms[0].name="myForm1";
document.forms[1].name="myForm2";
0
 

Author Comment

by:comcar
ID: 21780133
I think I see what the problem is now, the normal Javascript code ignores the changes made by jQuery.

So I need to make the changes initially using "document.getElementById..." and then refer back to them in the same way, and I can only access the jQuery labelled form "dervForm" if I use jQuery functions to call it.

So is the jQuery/$ object just like a mask for the HTML and JS below? In IE at least, as Firefox is happy for me to jump between the 2.
0
 

Author Comment

by:comcar
ID: 21780264
Ah no load of nonsense I'm talking.

document.forms[1].name="dervForm"; - still leads to the same thing, I suppose this is because the page is being loaded with the form, the array of 2 proDeps is created, then I'm trying to mess around with it.

I'm going to try to use REGEX to find "form... name="proDep" and change it for "form...name="dervForm" when it comes back from the AJAX call and before it's written to the page
0
 
LVL 82

Assisted Solution

by:hielo
hielo earned 100 total points
ID: 21780940
>>So I need to make the changes initially , using "document.getElementById..."
but I thought your <form> tags have NO id. There should be NO DUPLICATE ids throughout the page. If you need to assign ids to the form try:

document.forms[0].id="id1";
document.forms[1].id="id2";
0
 

Accepted Solution

by:
comcar earned 0 total points
ID: 21811051
Sorry that was just me making a mistake, my point was that I thought I'd have to make the changes using the standard "document..." notation instead of the jQuery "$('...')" notation. But it didn't work anyway.

Instead I used a javascript ".replace()" call to find "name='proDep'" in the response text and replace it with "name='dervForm'" so IE doesn't get the chance to make the array as the form arrives at the page renamed already.

Thanks for you help guys but I the only easy way to solve it was to change the form before it arrived at the page.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 7 to Windows 10 and back. IE 9.0, 10.0, 11.0 10 67
arrays and radio buttons 10 39
Echo'd values in dropdowns 6 29
Put javascript to get dropdown value 3 22
#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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)

749 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