We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Javascript remove characters from string

Medium Priority
1,380 Views
Last Modified: 2012-05-06
The below code does not work but I need it to badly.  The problem is something with the inline javascipt if i use this code outside of the div and just use the normal <Script type="text/javascript"> tags it works fine.  

Any ideas or solutions for me, I would really appreciate it.
<div id="javascript:var str ="01/01/2009"; var newStr = str.substring(6);  
document.write( 'a' + newStr );"
 
the end result that i need outputted is just <div id="a2009"

Open in new window

Comment
Watch Question

CERTIFIED EXPERT

Commented:
You have to put the Javascript in a script tag, but you can't have a script tag inside the attribute of an html tag, so you have to write all of the div tag using Javascript:
<script type="text/javascript">
var str = "01/01/2009";
var newStr = str.substring(6);
document.write('<div id="a' + newStr + '">');"
</script>

Open in new window

You cannot do it this way

one way is this



<div id="_div">
 
</div>
<script type="text/javascript"> 
var str ="01/01/2009"; var newStr = str.substring(6);  
var mydiv = document.getElementById("_div");
mydiv.id =  'a' + newStr ;
 
alert(mydiv.id);
</script>

Open in new window

Author

Commented:
Thanks for your help I didn't realize things would need to change that much. Additionally I had a class that needs to be changed to.  I have attached the full source, could someone please fix it for me

Could you please test your code to make sure it works I couldn't tell if they were doing what they needed to because when i viewed the source it just showed me the javascript

Thanks so much!
<div id="javascript:var str ="01/01/2009"; var newStr = str.substring(6);  
document.write( 'a' + newStr );" class="avascript:var str ="01/01/2009"; var newStr = str.substring(4);  
document.write( 'a' + newStr );">

Open in new window


<script type="text/javascript"> 
var str ="01/01/2009"; 
var newStr = str.substring(6);
var newCStr = str.substring(4);
var div = 'a' + newStr;
var cls = 'a' + newCStr;
 
document.write("<div id=\"" + div + "\" class=\"" + cls + "\" >");
</script>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Perfect, this is just what i needed!
CERTIFIED EXPERT

Commented:
If you use FireBug in Firefox, it will show you both the script and the generated code.

Use the substr function instead of substring. It's an undocumented feature that substring can be used with only one parameter...
<script type="text/javascript">
var str = '01/01/2009';
document.write('<div id="a' + str.substr(6) + '" class="a' + str.substr(4) + '">');
</script>

Open in new window

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.