Reputation: 855
How I can get a "Some text" from span with id="old-[id]" and put it in id="new-[id]" ?
<span id="old-1">Some text</span>
<span id="new-1"></span>
<span id="old-5">Some text</span>
<span id="new-5"></span>
I don't know how to get a digital part of id
without substring()
function.
I think exists is more correct solution.
Thanks.
Upvotes: 3
Views: 10118
Reputation: 616
I would use:
$(document).ready(function(){
var spanText = $('#old-1').text();
$('#old-1').text(null);
$('#new-1').text(spanText);
});
That will take the text from old and put it in new. If you have to do it will multiple id's then you will just have to write a loop to go through it or use the $.each method to iterate through all the id's.
Upvotes: 0
Reputation: 150030
If you want the contents of all of the old-n
spans to be copied to the corresponding new-n
spans then this should do it:
$('span[id^="old-"]').each(function() {
$('#new-' + this.id.split('-')[1]).html( $(this).html() );
});
Demo: http://jsfiddle.net/nnnnnn/bcXFf/
(Normally I would use .substr()
to do this since you already know the position within the string where the number occurs, but since you've asked to avoid it I've used .split()
instead.)
^=
is the "attribute starts with" selector, so the above code selects all spans with an id
that starts with "old-" and then for each one it finds the corresponding "new-" span and copies the content into it.
Upvotes: 1
Reputation: 707326
As your question is not entirely clear what you're asking, I'll offer you a bunch of different examples:
For a single id, you could do this:
$("#new-1").text($("#old-1").text());
If you have a bunch of sequential IDs that you want to do this on, you could do this:
for (var i = 1; i <= 5; i++) {
$("#new-" + i).text($("#old-" + i).text());
}
If it's HTML, not just text, you can use .html() in place of .text() in the above code.
$("#new-1").html($("#old-1").html());
or
for (var i = 1; i <= 5; i++) {
$("#new-" + i).html($("#old-" + i).html());
}
If you want to find all objects that have IDs that start with "old-" and process all of them regardless of how many and what numbers are on them, you could do this:
$('[id^="old-"]').each(function() {
var newid = this.id.replace(/old/, "new");
$("#" + newid).html($(this).html())
})
This will create a jQuery object of all objects that have an id that starts with "old-" and then replace the "old" with "new" to create the destination id value and use that to copy the HTML from the source to the destination.
If you have a given object in a variable named elem
and you want to extract just the numeric portion of the id value, you can use a regular expresssion to match the numeric portion like this:
var matches = elem.id.match(/\d+$/);
if (matches) {
var numString = matches[0];
}
Upvotes: 8
Reputation: 10508
I believe you want to retrieve an id without knowing it before-hand so that you can duplicate the text of that element.
If that's correct, this short jQuery function will do so.
$('span').each(function()
{
var idnum = $(this).attr('id').match(/[\d]/);
$('#new-' + idnum).text($(this).text());
}
);
I have created a jsfiddle to demonstrate.
Upvotes: 4
Reputation: 1517
Use something like this
$(document).ready(function () {
var el = $("#old-1");
var oldId = el.attr('id').replace(/[A-Za-z$-]/g, "");
$("#new-" + oldId).text(el.text());
});
Upvotes: 0
Reputation: 1084
The JavaScript paseInt() function will return only the integer part of a mixed string/integer variable. If you store the value of $('#old-1).attr('id') in a variable then parseInt on it, it should return just the number.
Upvotes: 0
Reputation: 30135
is this what you're looking for?
var id = 1;
$('#new-'+id).text($('#old-'+id).text());
Upvotes: 0