Javascript: Replace Text in an Element [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I've been working now for a while on the javascript process and can't really figure out on how to do this. I know how to basically tell javascript to set a certain value for a specific ID. But my Problem here is:
I'd like to write javascript to read out information (Server based, but that I'll do by myself ofc.) set it as var and then write it into text. It should look like this in the end:
Before writing (Without Javascript):
<h1 id="name" class="normal">Welcome, %Name%! What do you wish to do?</h1>
After writing (With Javascript):
<h1 id="name" class="normal">Welcome, John Connor! What do you wish to do?</h1>
So basically what I thought of is, that JS reads out information from server, then finds the h1 ID, searches in that text the %Name% value, and replaces it with the found name.
Please help me out here.Thanks!
Best Regards
Kodaigan

If using jQuery:
$('#name').html(function(index, html){
return html.replace('%Name%', yourVariable);
});
Pure javascript:
document.getElementById('name').innerHTML = document.getElementById('name').innerHTML.replace('%Name%','John');
This will replace a given string inside your h1 element.

If you have many paramaters, is better to use one of js lib's with templating.I prefere to use underscore.
http://underscorejs.org/#template. Also you can use if statements in youe template
How to use if statements in underscore.js templates?

Related

Find a gibberish text within body using javascript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to find a gibberish text that appears in the body without any HTML tags wrapping it. The source of this text is unknown. Not sure which approach would be better to find and remove it of the page Javascript
example:
<body>
<div>section</div>
Qui3Er3^6k
</body>
I am trying to find the string Qui3Er3^6k using javascript and remove it off the dom.
Note: I am not using jQuery, would like to achieve it with Javascript
If you're looking to remove Qui3Er3^6k from the DOM, you can use String.prototype.replaceAll():
document.body.innerHTML = document.body.innerHTML.replaceAll("Qui3Er3^6k", "");
<body>
<div>section</div>
Qui3Er3^6k
</body>

Jquery selector on a variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to use jQuery to select attributes and elements of some html code, alter them with jQuery, and then append that edited html code in the DOM.
I want to achieve something like this:
var codeExample = $(".stuff").html();
$(codeExample).attr("id").val("2412");
$(codeExample).find("div #foo").attr("rel").val("bar");
$("#element").append(codeExample);
Is that possible? Or I first have to insert the html code in the DOM, and then find it with jQuery and edit it?
var codeExample = $(".stuff");
codeExample.attr("id", "2412");
codeExample.find("div#foo").attr("rel", "bar");
Though your id and rel are already modified. But if you still want to append it to #element do the following:
$("#element").append(codeExample);
As Rory states in the comment, your code will work and should have no problems, but as you want to replace the content already placed in DOM, it might be useful to use $('.stuff').replaceWith(codeExample); instead.
You can find more about replaceWith() in the jQuery documentation page.

Possible to make an "alias" or "macro" to call a JavaScript function in HTML? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm writing a notebook in HTML for my own personal use. When writing an HTML document, you can't simply indent paragraphs with tabs or spaces. Rather than having to copy-and-paste several entities every time I want to indent a paragraph, I thought I'd be clever and write a JavaScript function to print 4 spaces. Unfortunately, I'll still end up copying and pasting <script>tab_function();</script> every time I want to indent something, which in that case I might as well just paste the entities instead.
I'm wondering if there is a way to create a "macro" or alias (like #define in C++) to make this less of a burden. It would be nice if I could #define "TAB" to call this function, but alas I don't know if such a thing exists in HTML. Is this even possible?
You can simply write a event listener into your html.
document.onkeydown = function(e) {
if(e.keyCode === 9){//9 = tab
//add 4 spaces, textarea.value += " "; perhaps?
e.preventDefault();
}
}
About macros, you can even sorta write your own compiler in javascript(compile to javascript I mean by that), by simply parsing text, basically anywhere.

PHP: Detect if string contains JavaScript/jQuery code inside and delete that part of code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a variable in PHP which contains different HTML code. Sometimes i find in this HTML code parts of javascript code or jquery code. This extra code may or may not have the <script> tag. So for example i might find jquery functions surrounded in a <p> tag.
Is there a way to detect this javascript/jquery code in the string and delete?
Yes, you can use HTMLPurifier to scrub bad code out of user input.
http://htmlpurifier.org/

Add just a </div> using JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm going crazy! :)
I need a way to add a closing </div> before another element. But everything I try the </div> gets filtered out. The problem is, that this might be the only solution for me.
So please, please have a look to this and maybe you're able to give me a hint:
I'm building a bootstrap based template for LimeSurvey, a online survey tool. Their templates are completely done with tables and I try to find another way and to get it mobile friendly.
The template is separated into different files. For my issue this is:
-> Startpage
-> Navigator
-> Endpage
Normally it loads always a "Surveypage" between Startpage and Navigator. But there is an option which automatically puts all question directly under the startpage and therefore into my header. So in this case I need another '' or another way to close the header first.
So there's a
<div class="jumbotron">
and I have to close it before the element
<table id="ls-table" ... >
I already tried many JavaScript examples I've been able to find around the web. But none makes the job.
Thanks a lot!
Philipp
There are only two ways to manipulate a web page using JavaScript.
The DOM, or document object model.
Via string-manipulation of the HTML.
Most web browsers will NOT allow you to do #2 directly on an already-loaded or loading document. At best, you could find a situation wherein you read the HTML of a <body> and then re-parse it. But doing so is an amazing amount of work for very little effort.
Look into the insertBefore method on the DOM, which will let you grab that <table id="ls_table" > element and move it from within that <div> to being a child of said <div>'s parent, immediately after the offending element.

Categories

Resources