JavaScript Adding attribute value to each <p> set attribute value jquery,set attribute value javascript,set attribute

I'm having struggle accomplishing this :

I want jQuery to grab class name of each icon and write it in each p element

HTML

<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div class="container-fluid">
    <div class="text-center">
        <div class="row">
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-1"></i></div>
                <p></p>
            </div>
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-2"></i></div>
                <p></p>
            </div>
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-3"></i></div>
                <p></p>
            </div>
        </div>
    </div>
</div>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>

jQuery

function idk() {
    var icon_class = $(".icon i").attr("class"),
        icon_container = $(".col-md-4"),
        icon_p = $('.col-md-4 p');
        icon_p.each(function(){
            icon_p.text(icon_class);
        })
};
$(document).ready(function () {
    idk();
});

JsFiddle

https://jsfiddle.net/277gpp7r/2/

This code only duplicates the first icon class name and writes it in all "p" elements instead grabbing the each class name, but this is not what I'm trying to accomplish.

I would appreciate any help, thanks in advance everyone!

Answer:1

function idk() {
  var icon_class = $(".icon i").map(function(){
  return $(this).attr("class")
  }).get().join( " " )
    icon_container = $(".col-md-4"),
    icon_p = $('.col-md-4 p');
  icon_p.each(function() {
    icon_p.text(icon_class);
  })
};
$(document).ready(function() {
  idk();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container-fluid">
  <div class="text-center">
    <div class="row">
      <div class="col-md-4">
        <div class="icon"><i class="icon-class-1"></i></div>
        <p>1</p>
      </div>
      <div class="col-md-4">
        <div class="icon"><i class="icon-class-2"></i></div>
        <p>2</p>
      </div>
      <div class="col-md-4">
        <div class="icon"><i class="icon-class-3"></i></div>
        <p>3</p>
      </div>
    </div>
  </div>
</div>
Answer:2

Try some thing like this.

function idk() {
        $('.col-md-4').each(function(){
        var icon_class = $(this).find(".icon i").attr("class");
        console.log(icon_class);
        icon_container = $(".col-md-4");
        icon_p = $(this).find('p');
             icon_p.text(icon_class);
        });
};
$(document).ready(function () {
    idk();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div class="container-fluid">
    <div class="text-center">
        <div class="row">
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-1"></i></div>
                <p></p>
            </div>
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-2"></i></div>
                <p></p>
            </div>
            <div class="col-md-4">
                <div class="icon"><i class="icon-class-3"></i></div>
                <p></p>
            </div>
        </div>
    </div>
</div>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" target='_blank' href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</body>
Answer:3

You can do like this

function idk() {
  var icon_p = $('.col-md-4 p');
  icon_p.each(function(i, v) {
    find the i & its class in previous div and add it t the element
    var findClass = $(this).prev('div.icon').find('i').attr('class');
    $(this).attr('class', findClass);
  })
};

DEMO

Answer:4

I have the following code: http://jsfiddle.net/chris_bentley/21onnz5a/ <ul id="header-master-fixed-icon"> <li><a href="#" id="header-master-fixed-search" ></a></li> &...

I have the following code: http://jsfiddle.net/chris_bentley/21onnz5a/ <ul id="header-master-fixed-icon"> <li><a href="#" id="header-master-fixed-search" ></a></li> &...

  1. click show hide div
  2. click show hide div jquery
  3. click show hide angular
  4. click show hide javascript
  5. click show hide
  6. click show hide div using css
  7. click show hide css
  8. click show hide toggle
  9. click show hide div jquery demo
  10. button click show hide div
  11. ng-click show hide
  12. button click show hide div in angular 6
  13. vue click show hide
  14. click toggle show hide jquery
  15. checkbox click show hide div
  16. button click show hide div jquery
  17. ng-click show hide div
  18. button click show hide div in angularjs
  19. on click show hide div javascript
  20. js click show hide

I'm trying to embed a Juicer.io social feed on a page that uses bootstrap tooltips. For some reason, loading the juicer embed JS (//assets.juicer.io/embed.js) breaks the JS that initializes tooltips ...

I'm trying to embed a Juicer.io social feed on a page that uses bootstrap tooltips. For some reason, loading the juicer embed JS (//assets.juicer.io/embed.js) breaks the JS that initializes tooltips ...

I am working on a d3 visualization to create a sunburst. I am also adding a functionality to search for a particular arc so that the sunburst displays arcs with only the searched label. If the ...

I am working on a d3 visualization to create a sunburst. I am also adding a functionality to search for a particular arc so that the sunburst displays arcs with only the searched label. If the ...

  1. filter javascript object by value
  2. filter javascript object by key
  3. filter javascript object array
  4. filter javascript object
  5. filter javascript object by property
  6. javascript filter object by key value
  7. javascript filter object array by property value
  8. javascript filter object by property value
  9. filter object javascript es6
  10. filter object javascript lodash
  11. javascript filter object by multiple values
  12. javascript filter object attributes
  13. javascript filter object contains value
  14. javascript filter object by key name
  15. javascript filter object list
  16. javascript filter object array by key
  17. javascript filter object by multiple keys
  18. javascript filter object based on value
  19. javascript filter object entries
  20. javascript filter object example

I am currently studying JavaScript and I have the following problem. Is it possible to get only the text from a div which has children inside it? I managed to make it work only for the text which ...

I am currently studying JavaScript and I have the following problem. Is it possible to get only the text from a div which has children inside it? I managed to make it work only for the text which ...

  1. text from which country
  2. text message which
  3. which text from about russell best supports