-4

For example ,

Consider i have the below code in a div

var qty = document.getElementsByName('subForm_prodQtyForm')[0].value; 
var rate = document.getElementsByName('subForm_rateForm')[0].value;

I need Javascript code to replace the above code as below code . How to replace ??

var qty = document.getElementsByName('subForm_prodQtyForm_1')[0].value;
var rate = document.getElementsByName('subForm_rateForm_1')[0].value;
karthi
  • 11
  • 6
  • @mplungjan If you look in the revision history you'll see that invalid code was posted and it was fixed by another poster. OP's "question" doesn't make much sense. If he needs to replace the above as something else, then that's a conclusion - not a question. Before it was hard to see *what* had changed (specially as it was in one line) – h2ooooooo May 27 '14 at 07:39
  • append _1 in that string – karthi May 27 '14 at 07:39
  • @karthi That isn't a question. What is your *question*? Are you asking if you have to append `_1` to the string? – h2ooooooo May 27 '14 at 07:39
  • He has some code in a string - he needs to add _1 to it at a specific place - what is there not to understand? – mplungjan May 27 '14 at 07:40
  • @mplungjan The fact that he's adding the characters inside the name of the element? Does he want `subForm_rateForm` there as well? From a logical standpoint the attempted code makes no sense at all. If he just wants to append `_1` to `document.getElementsByName('subForm_prodQtyForm')[0].value`, then that should be the question. – h2ooooooo May 27 '14 at 07:40
  • @mplungjan you are correct. any idea to solve this ?? – karthi May 27 '14 at 07:41
  • Why not use a text editor like vi, emacs, eclipse, etc. ? Are there many files to change? – Paul May 27 '14 at 07:41

2 Answers2

1

Assuming your "code" are just strings in a div and they do not change, you can do

var div = document.getElementById("divId");
var text = div.innerHTML;
div.innerHTML=text.replace(/Form'/g,"Form_1'");
mplungjan
  • 169,008
  • 28
  • 173
  • 236
0

If you want to append _1, then you can use

str='subForm_prodQtyForm'+'_1';
var qty = document.getElementsByName(str)[0].value;

Or, If you need to replace that string with some other, You can use the following:

str="subForm_prodQtyForm";
strlbl = str.replace('subForm_prodQtyForm','subForm_prodQtyForm_1'); 
var qty = document.getElementsByName(strlbl)[0].value;
Belinda
  • 116
  • 13
  • But if this is actually OPs question, why not simply replace the string in the code? Why use `document.getElementById('foo' + (true ? 'bar' : 'rab') + (10 + 113))` instead of `document.getElementById('foobar123')` when the result will *always* be the same? The simplest answer in this case would be "*move your cursor to right where you want to add the text, click and press the `_` button on your keyboard followed by `1`*". – h2ooooooo May 27 '14 at 07:48
  • @h2ooooooo Yes. we can replace it directly. But If the user wants to use the replaced string in particular places only, it can be used like this..(using replace in client-side scripting).. But the suggestion you have given is simplest i think in this case. – Belinda May 27 '14 at 07:52