select all text in JavaScript [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 8 years ago.
Improve this question
i need to select all text in my html code and surround them with <span> tag in JavaScript
convert this code:
<!DOCTYPE html>
<html>
<body>
<h1>Heading</h1>
<p>paragraph.</p>
</body>
</html>
to this:
<!DOCTYPE html>
<html>
<body>
<h1><span id="text">Heading</span></h1>
<p><span id="text">paragraph.</span></p>
</body>
</html>
how can i do this?

You can wrap your text with this fonction :
$(selector).wrapInner( "<span class=\"text\"></div>");
where selector target the tag you want

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(e) {
$('html').prependTo('<script>');
});
</script>
</head>
<body>
hi
</body>
</html>

Related

JavaScript Onclick Function Fails to Execute [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 months ago.
Improve this question
I have a button that triggers an onclick function:
<!DOCTYPE html>
<body>
<h2>what can javascript do?</h2>
<p Id="demo"></p>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
<button type="button" onclick='document.getElementById("demo").innerHTML = "Hello Javascript!"'>Click Me!</button>
</script>
</body>
</html>
when clicking the button, I found that the onclick does not trigger. What might be the problem with my code?
You don't need jQuery
Use addEventListener instead and set an ID to your button Element
use textContent (instead of innerHTML)
Add the missing <html> tags etc
Use type="button" on a Button Element (since by default is of type "submit")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Demo</title>
</head>
<body>
<h2>what can javascript do?</h2>
<button type="button" id="demoButton">Click Me!</button>
<p id="demo"></p>
<script>
const elDemo = document.querySelector("#demo");
const elButton = document.querySelector("#demoButton");
elButton.addEventListener("click", () => {
elDemo.textContent = "Hello Javascript!"
});
</script>
</body>
</html>

how can i make this code better?because it doesn't work now [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 3 years ago.
Improve this question
I have written this simple code in visual studio but it doesn't work.
please help me :)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" lang="en">
</head>
<body>
<img src="NewFolder/001.jpg" width="100" height="500" alt="" border="0" onclick="showpicture1();" />
<script>
function showpicture1() {
imgMain.src = "NewFolder/001.jpg";
imgMain.width = 400;
imgMain.height = 300;
}
</script>
</body>
</html>
First of all your script tag is in the wrong place.
You should include it or inside your tags or after your like:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" lang="en">
<script>
function showpicture1() {
var imgMain = document.getElementById('main');
imgMain.src = "NewFolder/001.jpg";
imgMain.width = 400;
imgMain.height = 300;
}
</script>
</head>
<body>
<img id="main" src="NewFolder/001.jpg" width="100" height="500" alt="" border="0"
onclick="showpicture1();" />
</body>
</html>
Preferrably, I would rather put it inside the head tag for being able to do DOM manipulations and therefore validating it. I would also declare the variable as var imgMain and getting it through assigning an id to the img. I hope that helps.

How to add a HTML page to another HTML page

i am failing to make the import function work, i am trying to import latestfeatures.html to Untitled Document.html here is my sample code can anyone check it and tell me what is wrong.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link rel="import" href="/template/html/latestfeatures.html">
<script>
var link = document.querySelector('link[rel="import"]');
var content = link.import;
// Grab DOM from latestfeatures.html's document.
var el = content.querySelector('.latestfeatures');
document.body.appendChild(el.cloneNode(true));
</script>
</head>
<body>
<div id="includedContent"></div>
</body>
</html>
you can use html iframe as iframe. Here you can get details of iframe.
http://www.w3schools.com/html/html_iframe.asp

how to pick value in jquery [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 8 years ago.
Improve this question
i have build a small function whose work to get value in alert box but i want to pick only 5 character from starting. here is my function
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<script type="text/javascript">
$(function (){
$(".hhh").click(function(){
$(this).html()
})
})
</script>
</head>
<body>
<div class="hhh">dd:ff:aa</div>
</body>
</html>
You could use the substring method:
$('.hhh').click(function() {
alert($(this).html().substring(0, 5));
});
Obviously checking the length of the string before calling substring to ensure that it has at least 5 characters could be a good check to do.
Do you mean substr ?
$(this).html().substr(0, 5);

How can I detect browser support of Flash in JavaScript? [duplicate]

This question already has answers here:
Closed 11 years ago.
This my HTML code but if the browser does not support Flash then I want to replace the image in the Flash part
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
if (typeof navigator.plugins['Shockwave Flash'] !== 'undefined') {
alert('support');
} else {
alert('not support');
}
</script>
</head>
<body>
</body>
</html>
I would use one of the Flash Detection JavaScript libraries out there. This one works well.

Categories