0

I'm a newbie here.

I would like to insert a value from JS variable into a URL part of an tag.

<script language="javascript" type="text/javascript">
  var scrt_var = "xyz_nt"; 
</script

For example, take the above value and insert/replace in the below img src where it reads 'insert value here'.

 <TD>
   <img src="https://test.com/?target=alias(keepLastValue(aggregates.*.servera_*.abc-insert value here.errors)%2C%20'Errors')&preventCache=25308886"  height="250" width="620" />
 </TD>

Any help would be really appreciated.

1
  • add onclick function to the element and pass the url to it. Combine url and js variable (in your JS code) to proceed. Commented Feb 20, 2018 at 8:41

2 Answers 2

4

You can add an ID to your img and do something like:

<script>
    var scrt_var = "xyz_nt";
    var mylink = "http://your_url?your_var=" + scrt_var;
    document.getElementById('image').src = mylink;
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use regex replace

var scrt_var = "xyz_nt"; 
var image = document.querySelector("td img");
image.src = image.src.replace( /insert value here/, scrt_var );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.