Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

633
Views
¿Cómo agrego data-bs-target al botón usando Js?

En realidad, toda mi función está integrada en javascript y busqué en Google para agregar data-bs-toggle usando js, pero todos mostraban jquery. Aquí está mi código:

 function onload_function(params) { n = 100 for (let i = 0; i < n; i++){ elem = document.getElementById(i) //console.log(elem.textContent) try{ console.log(elem.textContent) new_elem = document.createElement('button'); new_elem.innerHTML = elem.textContent; new_elem.{{data-bs-target}} = '#support-tab-8'; <-- here new_elem.id = 'button-' + i.toString(); new_elem.classList.add("nav-link"); document.getElementById("dynamic-headings").appendChild(new_elem); } catch{ } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede acceder a los atributos de datos utilizando la propiedad .dataset y establecer el valor del atributo de datos que desea cambiar. Tenga en cuenta que, si bien los atributos de datos están escritos en kebab-case en línea en html, en Javascript se accede a ellos con camelCase . Por lo tanto, desea establecer el valor como este new_elem.dataset.bsTarget = "YourValue"

He adjuntado un fragmento que demuestra el procedimiento a continuación.

 // We want to add the EventListeners only if the DOM is fully loaded to ensure that no issues happen. document.addEventListener('DOMContentLoaded', function(){ // Just for the sake of demonstrating I've added a button with an onClick Event to trigger the // change of the Data Attribute. In your code the change within might happen at any point. document.getElementById('btnChange').addEventListener('click', function(){ // We need the NodeElement of the Element we want to change the Data Attribute for. const el = document.querySelector('.my-span'); // If the Element exists we then change the Data attribute by just accessing the Dataset // property and define a property in the Dataset Object which is called the same as the // Attribute we want to set. But again, keep in mind that we write the Attribute here in // camelCase if(el) el.dataset.bsTarget = "MyValue"; }) });
 .my-span[data-bs-target="MyValue"]{ background-color: red; }
 <div class="container"> <span class="my-span">My Span Element</span> <button id="btnChange">Change Data Attribute</button> </div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!