Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

372
Visualizações
How to check if element has any attributes in it?

I'm trying to make a conditional checker check whether the element has any attributes or not.

var result = parentElm.hasAttribute(); i tried using this but I'm not sure I want a specific attribute by name, I just want to check whether the element has any attributes or attribute like length, size, or idk.

copyFormatCommand.js

    const model = this.editor.model;
    const selection = model.document.selection;
    const parentElm = selection.focus.parent;
    this.isEnabled = !!parentElm;
  

Here is the HTML I'm using to test if there are multiple nested elements and want to check whether the textNode's parent element has any attributes HTML

<p style="font: 11pt Times New Roman, Times, Serif; margin: 0;" xvid="c0fc3b7176d15af5d8c5c488a7fa675e"><span xvid="6f8db94eb5824fc90d39a2d9a127a98d">Annual Fund Operating Expenses</span></p>
<p style="font: 11pt Times New Roman, Times, Serif; margin: 0;" xvid="c0fc3b7176d15af5d8c5c488a7fa675e">
    <span xvid="6f8db94eb5824fc90d39a2d9a127a98d"><span xvid="1">hello</span></span>
</p>
<p>hello</p>

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can do something like this. If the element has more than one attribute (since id is already one) returns true otherwise false.

function hasAttributes(id) {
 const attr = document.getElementById(id)
 
 if (attr.attributes.length > 1) {
   return true
 } else {
   return false
 }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

The Object.keys() method returns an array of a given object's own enumerable property names, iterated in the same order that a normal loop would.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use the element.attributes property to get attributes. MDN has an example https://developer.mozilla.org/en-US/docs/Web/API/Element/attributes#enumerating_elements_attributes

   function listAttributes() {
     var element = document.getElementById("myElementId");
     var result = document.getElementById("result");

     // First, let's verify that the paragraph has some attributes
     if (element.hasAttributes()) {
       var attrs = element.attributes;
       var output = "";
       for(var i = attrs.length - 1; i >= 0; i--) {
         output += attrs[i].name + "->" + attrs[i].value;
       }
       result.value = output;
     } else {
       result.value = "No attributes to show";
     }
   }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda