views:

3099

answers:

2

So far i was using Safari 4 for testing and debugging my current jQuery Plugin, but as i tried my code out in Firefox it started to complain about something within the JQuery-Framework: "setting a property that has only a getter". I tried to find out wich line exactly causes Firefox to complain and found out that this happens somewhere here**

$.fn.util.create_$dom = function(opt) {
 var $dom = {};
 $.each(opt.dom,function(name,val){
  console.log(name);
  var $elm = $('<div>');
  $.each(opt.dom[name],function(_name,_val){
   if(_name == 'tagName') $elm = $('<'+_val+'/>');
  });
                    console.log(name+': ok');
  $.each(opt.dom[name],function(_name,_val){             **here  
   switch(_name){                                     **here
    case 'className': $elm.addClass(_val);         **here
    default: $elm.attr(_name, _val);               **here
   }                                                  **here
  });
  $dom[name] = $elm;
  console.log(name+': ok');
 });
 return $dom;
};

options.dom looks like this:

 dom:{
  wrapper:{className:'wrapper'},
  inner:{tagName:'p',className:'test',test:'bla'}
 },
A: 

It looks like your trying to set the tagName of an element with this line

$elm.attr(_name, _val);

This of course is not possible as it is read only.

redsquare
..but it seemed to be possible in safari would you say there is a workaround for firefox?
Julian Weimer
safari probably just swallows the exception. You can always wrap it in a try catch but why not have another case statement for tagName that does not set the tagName since you have already created the appropriate element.
redsquare
why not just delete the first each and move the condition you have inside it to be a case statement. You will have to ensure that tagName is the first item in the array however. I would maybe look at the structure of your object however to avoid the possibility of this
redsquare
yeah kinda this is why i added this first each loop, i wanted to stay flexible within the object notation
Julian Weimer
Anyway. Mark it up:) as it answers the Q.
redsquare
mmh, not yet :)... because it didn't work for me so far, i tried to fix it in this way but firefox is still in a bad mood
Julian Weimer
Well, I identified where the issue was and why it was happening. The remit was not to provide a whole working solution;)
redsquare
ok sir, thx for your quick response anyway, and you kinda helped me of course, but i want to wait at least 1-2 days for other responses
Julian Weimer
A: 
case 'tagName': break;

... is the solution, but in order to thank redsquare for the hint i marked him up.

Julian Weimer