tochrene
10-23-2013, 09:38 PM
dear coder colleagues,
I'm working on a very simple CMS system. It's my intention to include simple styling codes <b></b> into the database.
The code below is just an example, the real code is generated by mysql and php,
that's why the id's of the textarea are the same. The example does not work, only the first textarea id can be changed.
Is there a way to use a code like getElementByID by selected/active textarea?
your help is much appreciated, thanks in advance!
<html>
<head>
<script type="text/javascript">
function CodeBold(tekstveld_naam){
// code for IE
var textarea = document.getElementById(tekstveld_naam);
if (document.selection){
textarea.focus();
var sel = document.selection.createRange();
if (sel.text != ''){
sel.text = '<b>' + sel.text + '</b>';
}
}
// code for Mozilla
var textarea = document.getElementById(tekstveld_naam);
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var replace = '<b>' + sel + '</b>';
if (sel != ''){
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}
}
function CodeH1(tekstveld_naam){
// code for IE
var textarea = document.getElementById(tekstveld_naam);
if (document.selection){
textarea.focus();
var sel = document.selection.createRange();
if (sel.text != ''){
sel.text = '<h1>' + sel.text + '</h1>';
}
}
// code for Mozilla
var textarea = document.getElementById(tekstveld_naam);
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var replace = '<h1>' + sel + '</h1>';
if (sel != ''){
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<div>
<a href="#" onClick="CodeBold('textarea');return false;"><img src="_images/bold.jpg"></a>
<a href="#" onClick="CodeH1('textarea');return false;"><img src="_images/h1.jpg"></a>
</div>
<br />
<textarea name="textarea" id="textarea" cols="45" rows="5">this is the text in the textarea</textarea>
</form>
<form id="form1" name="form1" method="post" action="">
<div>
<a href="#" onClick="CodeBold('textarea');return false;"><img src="_images/bold.jpg"></a>
<a href="#" onClick="CodeH1('textarea');return false;"><img src="_images/h1.jpg"></a>
</div>
<br />
<textarea name="textarea" id="textarea" cols="45" rows="5">this is the text in the textarea</textarea>
</form>
</body>
</html>
I'm working on a very simple CMS system. It's my intention to include simple styling codes <b></b> into the database.
The code below is just an example, the real code is generated by mysql and php,
that's why the id's of the textarea are the same. The example does not work, only the first textarea id can be changed.
Is there a way to use a code like getElementByID by selected/active textarea?
your help is much appreciated, thanks in advance!
<html>
<head>
<script type="text/javascript">
function CodeBold(tekstveld_naam){
// code for IE
var textarea = document.getElementById(tekstveld_naam);
if (document.selection){
textarea.focus();
var sel = document.selection.createRange();
if (sel.text != ''){
sel.text = '<b>' + sel.text + '</b>';
}
}
// code for Mozilla
var textarea = document.getElementById(tekstveld_naam);
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var replace = '<b>' + sel + '</b>';
if (sel != ''){
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}
}
function CodeH1(tekstveld_naam){
// code for IE
var textarea = document.getElementById(tekstveld_naam);
if (document.selection){
textarea.focus();
var sel = document.selection.createRange();
if (sel.text != ''){
sel.text = '<h1>' + sel.text + '</h1>';
}
}
// code for Mozilla
var textarea = document.getElementById(tekstveld_naam);
var len = textarea.value.length;
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
var sel = textarea.value.substring(start, end);
var replace = '<h1>' + sel + '</h1>';
if (sel != ''){
textarea.value = textarea.value.substring(0,start) + replace + textarea.value.substring(end,len);
}
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<div>
<a href="#" onClick="CodeBold('textarea');return false;"><img src="_images/bold.jpg"></a>
<a href="#" onClick="CodeH1('textarea');return false;"><img src="_images/h1.jpg"></a>
</div>
<br />
<textarea name="textarea" id="textarea" cols="45" rows="5">this is the text in the textarea</textarea>
</form>
<form id="form1" name="form1" method="post" action="">
<div>
<a href="#" onClick="CodeBold('textarea');return false;"><img src="_images/bold.jpg"></a>
<a href="#" onClick="CodeH1('textarea');return false;"><img src="_images/h1.jpg"></a>
</div>
<br />
<textarea name="textarea" id="textarea" cols="45" rows="5">this is the text in the textarea</textarea>
</form>
</body>
</html>