-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;
7
  • @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) Commented May 27, 2014 at 7:39
  • append _1 in that string Commented May 27, 2014 at 7:39
  • @karthi That isn't a question. What is your question? Are you asking if you have to append _1 to the string? Commented May 27, 2014 at 7: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? Commented May 27, 2014 at 7: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. Commented May 27, 2014 at 7:40

2 Answers 2

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'");
Sign up to request clarification or add additional context in comments.

Comments

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;

2 Comments

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 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.