/    Sign up×
Community /Pin to ProfileBookmark

Hi everyone ?

I have a problem in the script below

[code=php]<script type=”text/javascript”>
function fBold(){
var obj = document.getElementById(‘txtar’);
var txt = getNewStr(obj, “[B]”, “[/B]”);
obj.value = txt ;
}

function getNewStr(obj, stra, strb){
if(obj.value.length != 0){
if(document.selection) {
//do IE code
return document.selection;
} else if(obj.selectionStart) {
//do FF code
var start = obj.selectionStart;
var end = obj.selectionEnd;
if (start != end){
var newstr = obj.value.substring(0, start) + stra +
obj.value.substring(start, end) + strb +
obj.value.substring(end);
return newstr;
}else{
return obj.value;
}
}
} else{
return ”;
}
}
</script>[/code]

The obj is a textarea and the script wraps [B] tags around it. It works fine, as long as I select from the second char or further. When the selection starts at the first char, I get ‘undefined’ in the textarea.

Can anyone solve this problem?
Thanks in advance

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Logic_AliJun 24.2007 — Think very carefully about what this is asking:
[CODE]if(obj.selectionStart)[/CODE][/quote]and what is returned if it fails.
Copy linkTweet thisAlerts:
@TheBigBauthorJun 24.2007 — Maybe I'm completely stupid, but I'm not really getting your point...

Could you be less cryptic please?
Copy linkTweet thisAlerts:
@Logic_AliJun 24.2007 — Maybe I'm completely stupid, but I'm not really getting your point...

Could you be less cryptic please?[/QUOTE]
You need to bear in mind the criteria that decide whether the condition of an [I]if[/I] statement evaluates as [I]true[/I], or [I]false[/I], how the expected range of values impact upon that, and what the function returns if the test condition is not met.
×

Success!

Help @TheBigB spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...