Jquery selector on a variable [closed] - javascript

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.

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>

Javascript: Replace Text in an Element [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'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?

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/

How to add script tag dynamically with custom src? [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
Keep getting this error: Uncaught TypeError: Cannot set property 'innerHTML' of null
I tried it all window.onload() plugins etc... nothing worked. I need to add a script tag with url that generates its own html and js in a div. please help!
var url = "http://static.polldaddy.com/p/0000000.js";
var pdScript = $('<script/>', {src : url});
$('body').append(pdScript);
Well, for a start, you're appending pdCsript but your variable is called pdSript. If you correct that, it works (barring something you haven't mentioned).

How to make a JS that put html code on a page [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 want to make a .js file that will store it seft some html tables and put the contents on the page that include .js file
how i can do that? this could replace some iframes roles.
Are you looking for innerHTML tag of Javascript.
Like this:-
document.getElementById('id').innerHTML = '<ol><li>html data</li></ol>';
You mean this?
document.body.innerHTML = '<p>Some html code</p>';

Categories

Resources