Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  149] [ 4]  / answers: 1 / hits: 17227  / 15 Years ago, mon, november 16, 2009, 12:00:00

I get the name of an input element, which is a string with a number (url1). I want to increment the number by 1 (url2) in the easiest and quickest way possible.



My way would be to get d / restofstring, ++ the match, then put together number with restofstring. Is there a better way?



Update:



My final (dummy)code became:



var liNew = document.createElement('li'); 
liNew.innerHTML = liOld.innerHTML;
var els = Y.Dom.getChildrenBy(liNew, function(el) {
return el.name.match(/d+$/);
} // YUI method where the function is a test
for (var i = 0, el; el = els[i]; i++) {
el.name = el.name.replace(/d+$/, function(n) { return ++n });
}
list.appendChild(liNew);

More From » html

 Answers
80

How about:



'url1'.replace(/d+$/, function(n){ return ++n }); // url2
'url54'.replace(/d+$/, function(n){ return ++n }); // url55


There we search for a number at the end of the string, cast it to Number, increment it by 1, and place it back in the string. I think that's the same algo you worded in your question even.



Reference:




[#98302] Wednesday, November 11, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaelynncherokeeg

Total Points: 697
Total Questions: 109
Total Answers: 104

Location: France
Member since Thu, Mar 18, 2021
3 Years ago
jaelynncherokeeg questions
Thu, May 27, 21, 00:00, 3 Years ago
Fri, Jan 24, 20, 00:00, 4 Years ago
Thu, Nov 14, 19, 00:00, 5 Years ago
Wed, Sep 18, 19, 00:00, 5 Years ago
;