Reputation: 119
I need to replace a part of a button URL in a data attribute.
I'm trying to do it like this:
$(elem).data('reditcontroller').replace('&id=', '&id=' + id);
But the data attribute of the button (elem) stays the same.
What am I doing wrong?
P.S.
I've also tried direct assignment using =
and still not getting the expected result.
Upvotes: 2
Views: 267
Reputation: 9561
You need to assign the value back to the attribute!!
$(function() {
var id = "test1";
$('#elem').attr('data-reditcontroller', $('#elem').attr('data-reditcontroller').replace('&id=', '&id=' + id));
console.log($('#elem').attr('data-reditcontroller'));
});
div {
min-height: 25px;
background-color: #d5d5d5;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="elem" data-reditcontroller="&id=">
</div>
Upvotes: 1
Reputation: 455
This just calculates the new url. You should set it back to the button like this:
var newData = $(elem).data('reditcontroller').replace('&id=', '&id=' + id);
$(elem).data('reditcontroller', newData);
Upvotes: 0