-1

On click I'm getting text in HTML. I have a variable in Javascript called content I want to get the text input and change a part of the Javascript variable.

HTML CODE:

 <html>
 <head>
 <link href="latent.css" rel="stylesheet">
 </head>
 <body>
 <button onclick="makePage()">Generate Link</button>
 Image link: <input type="text" name="img" id = "img"><br>
 Content: <input type="text" name="content" id = "content"><br>
 <script src="makePage.js">
 </script>
 <script>
 var img = document.getElementById("img").value;
 var content = document.getElementById("content").value;
 </script>

 </script>
 </body>
 </html>

JAVASCRIPT CODE:

function makePage(){

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200)
    alert("webpage " + xmlhttp.responseText + " was successfully created!");
}
var content = '<html><head><meta name="something:something" content=""></head><body></body></html>';
xmlhttp.open("GET","http://ahansabharwal.com/makePage.php?content=" + content, true);
 xmlhttp.send();
 }

In the variable called content I want to replace the image link in the content of the twitter:image meta name="twitter:image" content="http://graphics8.nytimes.com/images/2012/02/19/us/19whitney-span/19whitney-span-articleLarge.jpg"

AND

I would like to replace the content of the meta name="twitter:description" content="NEWARK - The guest list and parad[....] with the content inputted by the user.

0

1 Answer 1

0

I solved it by using replace();

What I did was:

 var contentJS = document.getElementById('contentInput').value;
 var contentNew = content.replace("info", contentJS);

I replaced the context to just info. Now it stores the value of the user input, then searches for the value of 'info' in the string. When it finds it, it simply gets replaced with the user input (contentJS)

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to consider expanding your answer somewhat, since presently it really lacks context/content and probably won't be useful to future readers.
Thanks for the suggestion. I have expanded.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.