Save a javascript object in an HTML object [duplicate] - javascript

This question already has answers here:
Can I add a custom attribute to an HTML tag?
(18 answers)
Closed 3 years ago.
Is there any way to save a javascript tag in an HTML object? What I would like to do is something like this:
<div id="htmlTag">some text</div>
<script type="text/javascript">
htmlTag.something = {s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}}
</script>

I think your only problem is you're not getting the node.
document.getElementById("htmlObject").something = {hi: 1};

You can convert JS objects to JSON string with this plugin, if that is what you want to achieve.
var json_data = JSON.stringify(yourObj);

You can do this using the javascript function JSON.stringify()
Javascript
<div id="htmlTag">some text</div>
<script type="text/javascript">
document.getElementById('htmlTag').something = JSON.stringify({s:"a string",n:1,b:false,a:[3,2,1],f:function(){alert("hello")}});
</script>
Here is a JSFiddle : https://jsfiddle.net/LcrLeg8a/1/

Related

Convert XML to HTML JS [duplicate]

This question already has an answer here:
Convert XML to HTML using jquery/javascript
(1 answer)
Closed 2 years ago.
I am a total 0 in JS but I got a task to write a simple function of converting XML to HTML
Example:
<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>
'Cat' should be in HTML
XML structure doesn't change but the content of text node does
Yeah I can google couple of hours how to get it or ask here and close the task, appreciate any help
The quickest is using jQuery
const xml = `<Translation code="200" lang="de-en">
<text>Cat</text>
</Translation>`
$("#content").text($(xml).find("text").text())
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="content"></div>

read file content on form input in angularjs [duplicate]

This question already has answers here:
ng-model for `<input type="file"/>` (with directive DEMO)
(13 answers)
Closed 4 years ago.
I have a JSON file in my HDD, how can I read the content of this file when I select it with input field?
I have to read the content and send content as data in some API!
I need something like this :
<input type="file" id="dashboardExportedFile">
<button type="button" ng-click="importDashboard">Import</button>
<script>
$scope.importDashboard = function (
var content = $("#dashboardExportedFile").content
console.log(content);
};
</script>
I think you need a Filereader. Follow my Link to see an working example with .txt File. Hope u can use it:
[JSFiddle][1]
[1]: http://jsfiddle.net/alexsuch/6aG4x/

Remove the contents under classname using Javascript [duplicate]

This question already has answers here:
Remove all child elements of a DOM node in JavaScript
(37 answers)
How do I clear the content of a div using JavaScript? [closed]
(2 answers)
Closed 5 years ago.
I want to remove the everything under the classname: social-share using JavaScript
HTML: This is what it looks like when I grab the code from chrome inspector.
<div class="social-share">
[shareaholic app="share_buttons" id="26444890"]
</div>
HTML: This is what exactly looks like inside the chrome inspector
<div class="social-share">
"
[shareaholic app="share_buttons" id="26444890"]
" == $0
</div>
You can use innerHTML to clear the contents of the element after it has been selected:
// select element with class 'social-share:
var social_share = document.querySelector('.social-share');
// Set it's contents to an empty string
social_share.innerHTML = '';
$('.social-share').empty();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="social-share"> content
<div>content</div>
</div>
Jquery empty() might be help you. It will remove every thing inside social-share class

How to get all the html inside a tag as it is using jquery? [duplicate]

This question already has answers here:
What's the right way to decode a string that has special HTML entities in it? [duplicate]
(7 answers)
Closed 6 years ago.
Consider this for example:
<div>
A&
<br>
B
</div>
If I use $('div').html(), it returns A&<br>B. It converts A& into A& which I do not want.
If use use $('div').text(), it returns A&B. It ignores the <br> which I do not want either.
I'm looking for a way to get all the html inside the div without parsing it and without skipping over the tags like <br> either.
This is the result I want: A&<br>B. How do I achieve that?
function decodeEntities(encodedString) {
var textArea = document.createElement('textarea');
textArea.innerHTML = encodedString;
return textArea.value;
}
console.log(decodeEntities($('div').first().html()))
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
A&
<br>
B
</div>
EDIT
See also node.innerText
https://developer.mozilla.org/en-US/docs/Web/API/Node/innerText
This is really a "hack" but it does work.
$("<textarea>").html($("div").html()).val()

document.getElementbyId in PHP? [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
I have a div that I want to put one image inside, how could I do something like that:
Javascript:
<script>
document.getElementById("people").innerHTML =
document.getElementById("people").innerHTML + "<img src='farmer.gif'>";
</script>
Html:
<html>
<body>
<div id = "people"></div>
</body>
</html>
in PHP? Im so fustrated I can do it in JS but I need to do it in PHP but I don't know how...
Hope you can help me! :D
Fast VERY DIRTY WAY:
do ob_start(); at the first line of the script
do echo "<div>%imag1flag%</div>";
do $content = ob_get_clean(); at the end of the script
change contnet with $content = str_replace('%imag1flag%','<img ...>',$content);
But wonder why you didnt paste the image in the first place eg:
echo "<div><img ...></div>";
Keep in Mind Javascript is Statefull PHP is NOT Statefull.
When u got that, maybe you will laugth about your Question ;)
Bye!

Categories