Reputation: 43
I am currently working on a little interface where I display an IP address and I am looking for a method to have it automatically trimmed in order to only showcase the last digits.
Example:
<p><strong>Room: </strong>123.123.123.101</p>
Ideal outcome:
<p><strong>Room: </strong>101</p>
Is there an easy method to have this achieved within jQuery?
Some expert advise would be greatly appreciated.
Thank you very much.
Upvotes: 0
Views: 253
Reputation: 18873
<p><strong>Room: </strong><span class='ip'>123.123.123.101</span></p>
$(document).ready(function(){
$('.ip').each(function(){
var data = $.trim($(this).html()); //using $.trim() here is optional as it is used to remove white spaces only.
data = data.substring(data.lastIndexOf(".") + 1);
$(this).html(data);
});
});
Upvotes: 0
Reputation: 597
if the format is permanent for
<p><strong>Room:</strong><span>123.123.123.101</span></p>
here is the simplest way
$('p').text().split('.')[3];
Upvotes: 1
Reputation: 4509
I'd suggest wrapping it in a seperate element to make the selection easier
<p><strong>Room:</strong><span>123.123.123.101</span></p>
After that, you can simply select the value, and use split
to transform the string into an array, which makes it easy to select the last digits by accessing the last element of the array.
var value = $('p span').html();
var valueArray = value.split('.');
value = valueArray[valueArray.length-1];
$('p span').html(value)
Upvotes: 0