Hi this seems to be a basic question but I can't get it right.
I have an xml which looks like this:
<xml id="emailBodyXML"><email><body></body></email></xml>
Now I have a variable, 'emailBody' that contains text values.
I want to be able to put the value of the emailBody inside the body tag. For example emaiLBody="Hello". I want my xml to look like this:
<email><body>Hello</body></email>
so how do i do it?
var emailBody = 'Hello';
var bodyTag = document.getElementsByTagName('body');
bodyTag[0].innerHTML = emailBody;
Related
I got some html code as a response from an ajax call. And I want to get the content of a specific div. Here's the html:
<html>
.
.
<div id="div-test">
.
.
</div><!--/div-test-->
.
.
</html>
Note: I use the <!--/div-test> because div#div-test contains more divs.
And that's my regex:
/<div[^.]*id=\"div\-test\"[^.]*>(.*?)<\/div><\!\-\-\/div\-test\-\->/
But it doesn't work at all. When I try to match it, all I get is a null value. So, is my regex wrong or is there anything else I need to do?
If you're looking for a non-regex approach, and you don't want to append the content on the page directly, you can create a document fragment and search through there:
var content = ""; // HTML FROM AJAX
var div = document.createElement('div');
div.innerHTML = content;
ajax_element = div.firstChild;
var test_content = ajax_element.getElementById('div-test').innerHTML;
as a regex approach, as much as I could advise against it, this might fit your needs:
var search_id = "div-test";
var r = new RegExp("<div[^>]*?id=[^\"]*?[^']*?"+search_id+"[^\"]*?[^']*?[^>]*?((?s).*)<\/div><!--\/"+search_id+"-->");
You can use the regex :
<div[^>]*?id='div-test'[^>]*?>(.*?)<\/div><!--\/div-test-->
Output
Or if the makup is with "" you can use
<div[^>]*?id=\"div-test\"[^>]*?>(.*?)<\/div><!--\/div-test-->
I have a file with this code:
var famname = guterriez;
And another HTML file family.html where I have included the location of the file in the head. I need to display the variable in a p element, and possibly in other locations on my site.
I would appreciate some help / tips on how to do this as I've done some research and nothing seems to be working.
If guterriez is not another variable but a string, then you have to put it into quotation marks. Like mentioned above.
Something like this might work for you, if you want to display the code in various locations on your page:
I.e. if you have a <div> or a <table> where you want to dispaly it:
<div id="MyDiv"></div>
<table id="MyTable"><tr><td id="MyCell"></td></tr>
The following JS Code will display the code in the mentioned above elements by calling them with their ID:
<script>
document.getElementById("MyDiv").innerHTML = famname;
document.getElementById("MyCell").innerHTML = famname;
</script>
Below is an example of how to integrate your JS file into your HTML, and reference variables defined there:
config.js
var txtVar = "This is some text";
page.html
<script src="config.js" />
<p id="textBlock"></p>
<script>
document.getElementById("textBlock").innerText = txtVar;
</script>
See code snippet below for a runnable example.
var txtVar = "This is some text";
window.onload = function() {
document.getElementById("textBlock").innerText = txtVar;
};
<p id="textBlock"></p>
I'm assuming guterriez is a string that you would like to assign to the variable famname. In that case, it needs to be enclosed in quotation marks to denote the fact that it is a string rather than a variable named guterriez (which presumably doesn't exist).
Try this:
var famname = "guterriez";
Then, you want to use innerText to assign this value to the p element in your HTML file. Try using document.getElementsByTagName("p")[index] to access the node and then assign innerText:
document.getElementsByTagName("p")[0].innerText = famname;
var famname = "guterriez";
document.getElementsByTagName("p")[0].innerText = famname;
<p></p>
This might seem a little simple, but i've tried many ways & non of them are working as expected.
i have values coming in from an ajax call, & i am displaying these to a <table>.
the data will not be seen at first (css - display:none) but onclick involves a function which displays a dialog of said data.
writing out the data in these ways does not work:
var text = "Example Data<br>";
var text = document.createTextNode("Example Data" + document.createElement('br'));
var text = document.createTextNode("Example Data");
text += document.createElement('br');
The latter outputs [object Text][object HTMLBRElement]
How do i write this correctly??
You can't concatenate node objects (trying to do so with + will convert them to strings first).
Find the element you want to append the nodes you've created, and call appendChild on it repeatedly.
var text = document.createTextNode("Example Data");
someElement.appendChild(text);
someElement.appendChild(document.createElement('br'));
You need to append the line break as an HTML element "createElement" as it is an HTML element.
var text = 'test';
var newtext = document.createTextNode(text),
p1 = document.getElementById("p1");
p1.appendChild(newtext);
p1.appendChild(document.createElement('br'));
p1.appendChild(document.createTextNode('newline displayed'));
Try
var p = document.createElement("p");
p.innerHTML = "Example Text<br>";
You can try this:
Give the table an id
Append html response to the table by using $('#tableid').html(responsedata);
I make an ajax call from my page , and then in response I also get some html like this
<parent id="1"><child></child></parent>
what I want is to get Inner HTML from the Response object excluding <parent>
How can I do that?
Cant use document.getElementbyID on a variable.
you can create a jQuery wrapper for the variable content and then extract the inner html using .html()
var data = '<parent id="1"><child></child></parent>'
var x = $(data).html()
Pure JS if you like ->
http://jsfiddle.net/eztZm/
//get
var get_html = document.getElementById("parent").innerHTML;
console.log(get_html);
//set
document.getElementById("parent").innerHTML = "new html";
https://developer.mozilla.org/en-US/docs/Web/API/Element.innerHTML
html code used:
<parent id="1"><child>candy</child></parent>
first approach:
var parent = document.getElementById("1");
var child_text = parent.firstChild.innerHTML;
to make a long story short:
document.getElementById("1").firstChild.innerHTML
will deliver "candy" (without jQuery) :)
I've got a string of html that I get via $("#datadiv").html();. Within this data are several other elements, and what I would like to do is append some data to one of those elements.
e.g.
var data = $("#datadiv").html();
var somestring = "Some text"
then append somestring into the div #stringholder inside of data. Is this possible?
And before the question comes, no I can't add it to the div before doing $("#datadiv").html();.
You can do something like this:
$(data).find("#stringholder").append(somestring);
As the html method returns a string, you need to pass it into jQuery again to create a jQuery object. You can then call find to get the element you want, and append to append the other string.
jQuery is quite happy to accept a string of HTML as an argument. It's not just selector strings that are accepted. If you pass in a string of HTML, that fragment will be the context for further method calls.
I think you already know this, but note that this will not affect the HTML in the DOM. It will only affect the fragment produced by passing the string into jQuery.
Do you mean :
var data = $("#datadiv").html();
var somestring = "Some text"
var newData = data + " " + somestring;
var holderData = $("#stringholder").html();
var newestData = holderData + " " + newData;
$("#stringholder").html('');
$("#stringholder").html(newestData);
Sure. Basically you could dump the string from the current div in to a variable and then concate the additional text and put it back in the div.
var someText = $('#datadiv').html()
var someNewText = 'my new text'
var someText = someText + ' ' + someNewText
$('#datadiv').html('') //this will clear the current text but not really necessary.
$('#datadiv').html('someText')
you just need to have some event that fires to trigger everything.