-1

Im trying to use string replace to change a line of code within my page.

<script type="text/javascript">
function replaceScript() {
    var toReplace = 'LINE OF CODE 333';
    var replaceWith ='??????????';
    document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}
</script>

How can I get my...

var replaceWith ='??????????';

...to equal the value of an input from a form on the page?

Note the input value is auto populated on page load and the user does not enter in there own email address.

Steve Brown
  • 115
  • 2
  • 11
  • 1
    This question could be simplified to ['how do I get the value of an input field'](http://stackoverflow.com/questions/11563638/javascript-get-input-text-value) correct? – Marty Jan 09 '15 at 03:27
  • well im trying to insert the replace with value equal to that of an inputs value – Steve Brown Jan 09 '15 at 03:29
  • Sure, but once you have the text from the input field it's not hard to use that as the value for `replaceWith`? – Marty Jan 09 '15 at 03:30
  • well there a little more...the inputs value holds the value of an email...so im trying to modify a scripts line to include this email value thats where the sting replace is comes in – Steve Brown Jan 09 '15 at 03:31
  • 1
    You're trying to do a find and replace on actual JavaScript code? Why would you need to do that? – Marty Jan 09 '15 at 03:32
  • no its for a mysql bit of code im trying to modify – Steve Brown Jan 09 '15 at 03:34
  • You've lost me entirely... Good luck though. – Marty Jan 09 '15 at 03:34
  • 2
    every comment you have added has made this less clear. –  Jan 09 '15 at 03:35
  • $query = "select Name from myTable where Name ='$NAME' "; you can use this in your sql and change the $NAME with the user input var – Fahad Rauf Jan 09 '15 at 03:35
  • "*Note the input value is auto populated on page load and the user does not enter in there own email address*" so why isn't this done at the server before the code reaches the client? Vastly more efficient and less problematic to do it there. – RobG Jan 09 '15 at 03:40

1 Answers1

0

I assume you have an input such as <input id="inputValue" type="text" />

The general approach is to use a DOM function like getElementById() or querySelector(), and access its value with the value property.

Your code could be as simple as:

<script type="text/javascript">
function replaceScript() {
    var toReplace = 'LINE OF CODE 333';
    var replaceWith = document.getElementById('inputValue').value;
    document.body.innerHTML = document.body.innerHTML.replace(toReplace, replaceWith);
}
</script>
bryc
  • 12,710
  • 6
  • 41
  • 61
  • i do believe this is what I'm looking for – Steve Brown Jan 09 '15 at 03:37
  • @Steve Marty already gave this answer in the first comment to your question – Fahad Rauf Jan 09 '15 at 03:38
  • 1
    Arbitrarily using a regular expression to replace markup in a page using the innerHTML property is not a particularly good idea. It will remove every event listener added by *addEventListener* and may have other undesirable consequences. – RobG Jan 09 '15 at 03:42