How Can I Use Html Tags In Javascript
Solution 1:
A sample data you get from server, and a sample html you want to add would make it easier for people to help you.
The basic steps are 1.Get a reference to the html node you want to put the new data in. There are multiple strategies to get reference to the node. If it has an id, it's most starightforward. 2.set innerHTML property.
eg
var node = document.getElementById("targetNode");
node.innerHTML = "<div>data</div>";
Solution 2:
Well... Not much detail so not much of an answer...
Easy way
document.write("<body> <div> this is on my page! </div> </body>
or you can edit the innerhtml of an element to place things inside it
document.getElementById("id").innerHTML = "<div>This is inside my element with id="id" </div>"
Answers the question, no?
Solution 3:
Instead of embedding html into javascript, you could make a bunch of predefined javascript functions and then use them outside of the script tags. For example, here's how to display a picture while still using the javascript functions.
<html><scripttype="text/javascript">functiondisplay_message()
{
alert("This is a message.");
};
</script><body><imgsrc="image.jpg"><form><inputtype="button"value="Click me!"onclick="display_message()" /></form></body></html>
Solution 4:
I know this is an old post, but this could be helpful... Using jquery is great way to combine html elements with script Note: This would be used in the body, if using it in the be sure to enclose it in
$(document).ready (function(){ YOURCODE });
// Create an object:var url = {
link: "https://stackoverflow.com/questions/2834929/how-can-i-use-html-tags-in-javascript"
};
// Display URL object:
$('#panel').html('<a href=' + url.link + '>' + url.link + '</a>');
//Note: the # denotes id, if you want to use a class it would be://$('.panel').html('<a href=' + url.link + '>' + url.link + '</a>');//your html would be: <div id="panel"></div> or whatever you choose ie <p> and so //forth
Post a Comment for "How Can I Use Html Tags In Javascript"