Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Create new element with data attribute

Please i am having difficulty creating the flexbox script below dynamically.

<div class="Table-row">
    <div class="Table-row-item u-Flex-grow2" data-header="Header1">row2 col1</div>
    <div class="Table-row-item" data-header="Header2">row2 col2</div>
    <div class="Table-row-item" data-header="Header3">row2 col3</div>
    <div class="Table-row-item" data-header="Header4">row2 col4</div>
    <div class="Table-row-item u-Flex-grow3" data-header="Header5">row2 col5</div>
    <div class="Table-row-item" data-header="Header6">row2 col6</div>
    <div class="Table-row-item" data-header="Header7">row2 col7</div>
  </div>

In my Jquery i loop through like below.

for (var i = 0 ; i < data.length ; i++){
   var className = metadata[i].toLowerCase().replace(/\s/g, "-") + " Table-row-item";
   var rowElement = $("<div></div>", {class: className, text: data[i]});

   $('.'+className).prop('data-header', 'value');
   rowElement.appendTo($tr);
}

The problem is

$('.'+className).prop('data-header', 'value');

does not add my data-header property. I tried adding it like

$("<div></div>", {class: className, text: data[i], data-header :'value'})

And it throws error.

I tried

$('.'+className).attr('data-header', 'value');

as explain Here as well and it isn't adding. Please how do i achieve or add the property dynamically ? Any help or suggestion would be appreciated.

like image 324
Nuru Salihu Avatar asked Nov 08 '15 13:11

Nuru Salihu


People also ask

How to add attributes to HTML element using jQuery ATTR () method?

This is the answer to add attributes to an HTML element using jQuery attr() method. The above example adds a class attribute to the HTML paragraph tag. The method adds the class attribute to the HTML element as well the styling of the specified class. Click the Add attribute button to see the changes.

Can you create an HTML tree with attributes using jQuery?

In fact, it can build any tree of HTML elements you want: You can also use this format to create an element with attributes: You can create a new jQuery element and set its attributes without having to build the full HTML string yourself. This is useful if you’re generating attribute values dynamically.

How do I add a new element using jQuery?

jQuery - Add Elements. With jQuery, it is easy to add new elements/content. Add New HTML Content. We will look at four jQuery methods that are used to add new content: jQuery append() Method. The jQuery append() method inserts content AT THE END of the selected HTML elements.

What are data-* attributes in jQuery?

Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element's data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).


2 Answers

wrap data-header in ' :-

$("<div></div>", { 'class': className, 'text': data[i], 'data-header' :'value'});

Another option is to set the rowElement:-

var rowElement = $("<div></div>", {class: className, text: data[i]});
rowElement.prop('data-header', 'value');

or

var rowElement = $("<div></div>", {class: className, text: data[i]}).prop('data-header', 'value');

$('.'+className) will only be available after you append.

Though you should use .data('header', 'value'); to set data.

like image 161
BenG Avatar answered Sep 26 '22 03:09

BenG


The following code work fine and add the data attribute to the element with class className :

$('.'+className).attr('data-header', 'value');

But in your case you can add rowElement because the variable is not added to the DOM :

$(rowElement).find('.'+className).attr('data-header', 'value');

Note : Better to use data instead of attr when you want set or get data attributes.

Hope this helps.

like image 20
Zakaria Acharki Avatar answered Sep 26 '22 03:09

Zakaria Acharki