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.
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:
If you have a bunch of sequential IDs that you want to do this on, you could do this:
If it’s HTML, not just text, you can use .html() in place of .text() in the above code.
or
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:
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
elemand 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: