Monday, January 31, 2011

Monday 1/31/2011


function setNumberOfSides() {
numSides = document.getElementById("numSides").value;
numSides = numSides + " sides per die";
var alertText = "";
var holderP = document.getElementById("numberOfSides");
alertText = document.createTextNode(numSides);

if (holderP.firstChild == null) {
holderP.appendChild(alertText);
}
else {
holderP.firstChild.nodeValue = numSides;
}
}

Here is something from the Shelley Powers book


<script type="text/javascript">
function init() {
var searchString = "Now is the time, this is the time";
var re = /t\w{2}e/g;
var replacement = searchString.replace(re, "place");
alert(replacement);
}
</script>

Here is another tutorial


<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Searching for Strings</title>
<style type="text/css">
#searchSubmit {
background-color:#ff0;
width: 200px;
text-align:center;
padding:10px;
border:2px inset #ccc;
}

.found
{
background-color: #ff0;
}

</style>
<script type="text/javascript">
window.onload=function() {
document.getElementById("searchSubmit").onclick=doSearch;
}

function doSearch() {
//get pattern
var pattern = document.getElementById("pattern").value;
var re = new RegExp(pattern, "g");

//get string
var searchString = document.getElementById("incoming").value;

// replace
var resultString = searchString.replace(re,"<span class='found'>$&</span>");

// insert into page
document.getElementById("searchResult").innerHTML = resultString;
}
</script>
</head>
<body>
<form id="textsearch">
<textarea id="incoming" cols="100" rows="10">

</textarea>
<P>
Search pattern: <input id="pattern" type="text" /></p>
</form>
<p id="searchSubmit">Search for a pattern


<div id="searchResult"></div>
</P>

</body>


Here is some more JavaScript


<script type="text/javascript">
var pieceOfHtml = "<p>This is a <span>paragraph</span

"
function convert(){
pieceOfHtml = pieceOfHtml.replace(/</g,"<");
pieceOfHtml = pieceOfHtml.replace(/>/g,">");
document.getElementById("searchResult").innerHTML = pieceOfHtml;
}
</script>

No comments: