Issue Background
I'm trying to add a popover to every element with class .param-value
. I want to do this via JQuery to make it more DRY.
Issue
How can I make the data-content
attribute equal to the content of the element?
For example
<p class="param-value" [...] data-content="Example Data 1">Example Data 1</p>
What I've tried
I have tried to use $(this).html()
but as this is not an event handler I believe cannot pull this information.
HTML
<div class="param-row row">
<div class="col-3"><p class="param-name">Example Label 1</p></div>
<div class="col-3"><p class="param-value">Example Data 1</p></div>
<div class="col-3"><p class="param-name">Example Label 2</p></div>
<div class="col-3"><p class="param-value">Example Data 2</p></div>
</div>
<div class="param-row row">
<div class="col-3"><p class="param-name">Example Label 3</p></div>
<div class="col-3"><p class="param-value">Example Data 3</p></div>
<div class="col-3"><p class="param-name">Example Label 4</p></div>
<div class="col-3"><p class="param-value">Example Data 4</p></div>
</div>
JS
$(function () {
$('[data-toggle="popover"]').popover({'trigger':'hover'})
})
$('.param-value').attr('data-container','body')
$('.param-value').attr('data-toggle','popover')
$('.param-value').attr('data-placement','bottom')
$('.param-value').attr('data-html','true')
$('.param-value').attr('data-content',$(this).html())
JSFIDDLE
https://jsfiddle.net/jx0csyLu/1/
CodePudding user response:
You can pass a function as a second parameter to the attr
method and inside that function $(this)
will be a reference to the current element and then you can just use $(this).html()
$(function() {
$('[data-toggle="popover"]').popover({
'trigger': 'hover'
})
})
$('.param-value').attr('data-container', 'body')
$('.param-value').attr('data-toggle', 'popover')
$('.param-value').attr('data-placement', 'bottom')
$('.param-value').attr('data-html', 'true')
$('.param-value').attr('data-content', function() {
return $(this).html()
})
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3 Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2 poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft 2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
<div class="param-row row">
<div class="col-3">
<p class="param-name">Example Label 1</p>
</div>
<div class="col-3">
<p class="param-value">Example Data 1</p>
</div>
<div class="col-3">
<p class="param-name">Example Label 2</p>
</div>
<div class="col-3">
<p class="param-value">Example Data 2</p>
</div>
</div>
<div class="param-row row">
<div class="col-3">
<p class="param-name">Example Label 3</p>
</div>
<div class="col-3">
<p class="param-value">Example Data 3</p>
</div>
<div class="col-3">
<p class="param-name">Example Label 4</p>
</div>
<div class="col-3">
<p class="param-value">Example Data 4</p>
</div>
</div>
CodePudding user response:
I was able to solve this with JQuery's each()
. Like so:
$('.param-value').each(function() {
$(this).attr('data-content',$(this).html())
})
If there is a more elegant solution, please let me know.