Shoutbox

Javascript Help [SOLVED!] - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: General (/forumdisplay.php?fid=11)
+---- Forum: General Chit Chat (/forumdisplay.php?fid=14)
+----- Thread: Javascript Help [SOLVED!] (/showthread.php?tid=76846)

Javascript Help [SOLVED!] by glennlopez on 08-18-2007 at 06:18 AM

Hey guys, im having trouble with javascript coding. I know for a fact that this comunity has a lot of genus coders which is why im asking here first.

Anyways, what i wanna acomplish is:

I've got a div tag with a content "hello world"
ie: <div>hello world</div>

Now what I wanna do is take out anything after 4 letters in that div tag
ie: so im left with the word 'hell' in the end. instead of 'hello world'

I know i can use:
x=x.substr(0,4);

to subtract anything after the 4rth letter but my question is... how do I apply this logic to what I wanna get acomplished?

could someone who knows what they're doing please complete the code for me so i can understand how its done right? Thanks everyone!

I've got a joost + pownce invite for the first person with the working script


RE: Javascript Help (pownce & joost invite reward) by markee on 08-18-2007 at 06:39 AM

code:
<script language="Javascript">var element = document.getElementById("IdOfTheDivYouWant");element.innerHTML = element.innerHTML.substr(0,4);</script>

Don't worry about the invites, it's not like it was difficult.
RE: Javascript Help (pownce & joost invite reward) by glennlopez on 08-18-2007 at 07:18 AM

hmm... it didnt work :(

<head>
<script language="Javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.substr(0,4);
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<div id="poop">hello world</div>
</body>


RE: Javascript Help (pownce & joost invite reward) by scott2010_h on 08-18-2007 at 07:34 AM

quote:
Originally posted by glennlopez
<head>
<script language="Javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.substr(0,4);
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="poop">hello world</div>
</body>
That won't work because the div is created after the script is called. So you need to place the code in after the div like this:
code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="poop">hello world</div>
<script language="javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.substr(0,4);
</script>
</body>
</html>


Tested it on my local server and it worked (Y)
RE: Javascript Help (pownce & joost invite reward) by scott2010_h on 08-18-2007 at 08:33 AM

sure I'll take a joost invite just sent it to [Image: sig.php?email=scott2010_h%20AT%20hotmail%20DOT%20com]
thanks glennlopez :)


RE: Javascript Help (pownce & joost invite reward) by glennlopez on 08-18-2007 at 09:01 AM

ahh ic, thank you scott for clearing that up :)

one more thing i forgot to mention... I need the same script to remove any numbers within that same div tag "poop".

18 August 2007 will turn into August
18 September 2006 will turn into Septeber
122 dDfs sstt eds 56 will turn into dDfs sstt eds
etc..

can any one help me with that one as well?


RE: Javascript Help (pownce & joost invite reward) by scott2010_h on 08-18-2007 at 09:18 AM

I think this is what you looking for

code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="poop">hello Wednesday</div>
<script language="javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.replace(/Wednesday/g, "Ooompa Loompa");
</script>
</body>
</html>
This code will replace all instances of Wednesday with Ooompa Loompa
RE: RE: Javascript Help (pownce & joost invite reward) by glennlopez on 08-18-2007 at 09:27 AM

quote:
Originally posted by scott2010_h
I think this is what you looking for
code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="poop">hello Wednesday</div>
<script language="javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.replace(/Wednesday/g, "Ooompa Loompa");
</script>
</body>
</html>
This code will replace all instances of Wednesday with Ooompa Loompa


dude, your the best! thanx, ill invite you to joost as soon as i open it :)

also... lol i dont meen to ask for milk but you think you could help me with this last one as well...

I need the same script to remove any numbers within that same div tag "poop".

18 August 2007 will turn into August
18 September 2006 will turn into Septeber
122 dDfs sstt eds 56 will turn into dDfs sstt eds
etc..
RE: Javascript Help (pownce & joost invite reward) by scott2010_h on 08-18-2007 at 09:50 AM

this should do it:

code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<div id="poop">18 August 2007</div>
<script language="javascript">
var element = document.getElementById("poop");
element.innerHTML = element.innerHTML.replace(/[*0-9]/g,'');
</script>
</body>
</html>

Edit:
btw thanks for the invite :)