Reputation: 482
Is it possible to insert, f.e. to "span", an attribute like this.
<span data-text="Hello world"></span>
If the event reaches its condition, then script takes information from "data-text" and inserts it into "span".
So, resulting span will be like
<span data-text="Hello world">Hello world</span>
Upvotes: 3
Views: 46
Reputation: 87203
Yes, you can use text
as follow
$('span[data-text]').text(function() {
return $(this).data('text');
});
$('span[data-text]')
will select all the <span>
elements having data-text
attribute present on themtext()
with callback function is used to iterate over all the matched elements and update their respective innerText
. You can also use html()
instead of text()
.$(this).data('text')
will retrieve the data-text
attribute value of the current($(this)
) element and returning it from the function will update the innerText.Demo
$('span[data-text]').text(function() {
return $(this).data('text');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<span data-text="Hello world"></span>
<span data-text="GoodBye World!"></span>
<span data-text="Is it Working?"></span>
Upvotes: 1