We help IT Professionals succeed at work.
Get Started

setAttribute problem in IE

Savitrikvl
Savitrikvl asked
on
504 Views
Last Modified: 2012-05-05
Hi,

I am facing problems with setAttribute function in IE.  I understand from what I read on the net that IE doesn't support it and will never support it.  I would like to understand if there is a solution for what I want to do that works both in IE (at least version 7 onwards) and Firefox.

My requirement is:

I am dynamically generating fields by using Javascript.  I need to assign properties like classes, names, IDs, events dynamically to these.  I am currently using setAttribute to do this.

Below is a sample code where I am using an alternate solution by hiding and showing hyperlinks.  While I can afford to do it for this example, I cannot in many places in the rest of my code.

So, if I can get help on satisfying my requirement in the below code without the alternate solution I am currently using, it will help me fix the rest of my code.
this HTML
<a href="javascript:void(0)"  id="edit" onclick="companyEdit();" >Edit</a>

is calling the function defined here:

<script language="javascript">
function companyEdit()
   {
       alert('edit company');
   document.getElementById('edit').innerHTML="Save";
   document.getElementById('edit').setAttribute("onclick", "saveCompany('save company');");
   }
function saveCompany(msg)
  {
    alert(msg);
  }  
</script>

I want to change companyEdit() to  saveCompany()  dynamically using setAttribute.
Above example is not working IE only.

For this i have  one alternative solution  

   i have another hyperlink  like
   <a href="javascript:void(0)" style="display:none"  id="save" onclick="saveCompany('save company');" >save</a>
   when somebody cick on edit link i am changing display:none to display:block.
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE