Problem with dynamic element creation

Asked

Viewed 24 times

0

I am developing an application that uses a lot of Jquery, since there are several very specific functions.

But for reasons of customization and optimization, I managed with the mix of some researches to create "my own lib" in a very simplistic way, nothing great, just in order to perform functions in the front end that style the site from a pattern of its own. The link to a part of it is here on github.

And created the function .newElement(), that in the case below works perfectly.

var div = M('div');
div.css({
  width: '100px',
  padding: '10px',
  background: "#333"
})

var a = div.newElement('a');
a.css({
  color: 'white'  
})
a.html('Exemplo')
a.attrs({
  href: "https://jsfiddle.net/",
  target: "_blank"
})
<script src="https://rawgit.com/samirbraga/libM/master/mlib.js"></script>
<div></div>

But when I try to raise more than one child to the same element, I have a problem:

var div = M('div');
div.css({
  width: '100px',
  padding: '10px',
  background: "#333"
})

var a = div.newElement('a');
a.css({
  color: 'white'  
})
a.html('Exemplo')
a.attrs({
  href: "https://jsfiddle.net/",
  target: "_blank"
})

var p = div.newElement('p');
p.html('Texto que não deveria ser adicionado ao link')
<script src="https://rawgit.com/samirbraga/libM/master/mlib.js"></script>
<div></div>

The new child, which by code should be added to the same element, is added to the child. In this case div > a > p, what should be div > a, p.

The strange thing is that if I don’t pass the element I want to add to a variable div = M('div') and instead use M('div').newElement('p'), works.

I don’t know what’s causing it.

The code of the function newElement():

newElement: function(element){
  var _newElement;
  each(self.myArg, function(el, i) {
    _newElement = document.createElement(element)
    el.appendChild(_newElement);
  })
  return M(_newElement)
},

From now on, thank you.

  • What is self.myArg?

  • @Sergio, it’s the general function argument. M('.div'), in the case .div would be returned in the self.myArg.

  • While I saw the Github link, I went to see there

  • It’s late here and I won’t be able to analyze much. But what is the idea of .myIndex?

  • @Sergio, It would be a second argument of the function, selects the element by the index. Ex M('div', 2). Use as another option for M('div').eq(2).

  • Okay, and why do you have var myArg;&#xA; var self; global? There is a great risk of being overwritten by methods on different elements...

  • @Sergio, that’s true. In this case, I don’t even use the myArg, but I use the self. How do you think you should go?

  • Creating a new library is complex. If you only have a few methods, fine. Otherwise it’s better to use something that already exists. And there’s a lot more out there than jQuery :P But here’s an idea of the approach you could take: https://jsfiddle.net/6883xzsh/

  • @Sergio, and I’ve had contact with some :P, the idea wasn’t even to create a whole lib, it was just the app’s own methods that I’m creating, like selects Places, etc. The real focus, it was just substituting elements, so the method I would use the most was this newElement. I’ll take a look at your fiddle.

  • @Sergio, I took a look. It’s, in your example, with class and the constructors are working fine. But I still haven’t been able to apply in mine, I think the problem is in the loop. Anyway, thanks for the attention. :)

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.