JavaScript execCommand bold command











up vote
-2
down vote

favorite












I am making this WYSIWYG editor using JavaScript's execCommand and later I will be customizing it. But I am having a problem. First of all here is the codepen.io link of my problem: https://codepen.iο/pankaj260/pen/BGJaWJ?editors=1010. And below is all the details of the problem.



Take this example string "pankaj is not feeling well".

Now if you see my JavaScript code I have used odd even concept to show bold button is active/inactive. And another if statement if the selected text is to be bolden and I don't increment my boldCounter variable(please see my javascript code). Now if I paste my example string in contenteditable div. Following happens.




  1. If I select text "pankaj" press bold button it makes it bold and bold button does not get yellow (great, awesome).


  2. Now without selecting any text if I press bold button the button turn yellow and if I write anything after "well" word it gets written in Bold (again awesome) and after writing some bold content I again press bold button the colour of button changes to white and if I write any thing onwards everything gets written in normal font.


  3. But if I select last word of my example string and press bold button it does make the last word bold i.e "well". But the color of the button does not change to yellow. And everything gets written in Bold. So now if I want to stop bolding it I again press bold button, now color changes to yellow but fonts are normal fonts. And from here things get messed up.



I want my bold button to work like pell










share|improve this question
























  • stackoverflow.com/questions/12734722/…
    – epascarello
    Nov 20 at 15:50










  • no this is not what i am trying to ask
    – pankaj roy
    Nov 20 at 15:53










  • well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
    – epascarello
    Nov 20 at 16:12












  • Then help me please . If you have your own logic that might work please suggest me .
    – pankaj roy
    Nov 20 at 16:21










  • You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
    – skyline3000
    Nov 20 at 18:47

















up vote
-2
down vote

favorite












I am making this WYSIWYG editor using JavaScript's execCommand and later I will be customizing it. But I am having a problem. First of all here is the codepen.io link of my problem: https://codepen.iο/pankaj260/pen/BGJaWJ?editors=1010. And below is all the details of the problem.



Take this example string "pankaj is not feeling well".

Now if you see my JavaScript code I have used odd even concept to show bold button is active/inactive. And another if statement if the selected text is to be bolden and I don't increment my boldCounter variable(please see my javascript code). Now if I paste my example string in contenteditable div. Following happens.




  1. If I select text "pankaj" press bold button it makes it bold and bold button does not get yellow (great, awesome).


  2. Now without selecting any text if I press bold button the button turn yellow and if I write anything after "well" word it gets written in Bold (again awesome) and after writing some bold content I again press bold button the colour of button changes to white and if I write any thing onwards everything gets written in normal font.


  3. But if I select last word of my example string and press bold button it does make the last word bold i.e "well". But the color of the button does not change to yellow. And everything gets written in Bold. So now if I want to stop bolding it I again press bold button, now color changes to yellow but fonts are normal fonts. And from here things get messed up.



I want my bold button to work like pell










share|improve this question
























  • stackoverflow.com/questions/12734722/…
    – epascarello
    Nov 20 at 15:50










  • no this is not what i am trying to ask
    – pankaj roy
    Nov 20 at 15:53










  • well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
    – epascarello
    Nov 20 at 16:12












  • Then help me please . If you have your own logic that might work please suggest me .
    – pankaj roy
    Nov 20 at 16:21










  • You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
    – skyline3000
    Nov 20 at 18:47















up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I am making this WYSIWYG editor using JavaScript's execCommand and later I will be customizing it. But I am having a problem. First of all here is the codepen.io link of my problem: https://codepen.iο/pankaj260/pen/BGJaWJ?editors=1010. And below is all the details of the problem.



Take this example string "pankaj is not feeling well".

Now if you see my JavaScript code I have used odd even concept to show bold button is active/inactive. And another if statement if the selected text is to be bolden and I don't increment my boldCounter variable(please see my javascript code). Now if I paste my example string in contenteditable div. Following happens.




  1. If I select text "pankaj" press bold button it makes it bold and bold button does not get yellow (great, awesome).


  2. Now without selecting any text if I press bold button the button turn yellow and if I write anything after "well" word it gets written in Bold (again awesome) and after writing some bold content I again press bold button the colour of button changes to white and if I write any thing onwards everything gets written in normal font.


  3. But if I select last word of my example string and press bold button it does make the last word bold i.e "well". But the color of the button does not change to yellow. And everything gets written in Bold. So now if I want to stop bolding it I again press bold button, now color changes to yellow but fonts are normal fonts. And from here things get messed up.



I want my bold button to work like pell










share|improve this question















I am making this WYSIWYG editor using JavaScript's execCommand and later I will be customizing it. But I am having a problem. First of all here is the codepen.io link of my problem: https://codepen.iο/pankaj260/pen/BGJaWJ?editors=1010. And below is all the details of the problem.



Take this example string "pankaj is not feeling well".

Now if you see my JavaScript code I have used odd even concept to show bold button is active/inactive. And another if statement if the selected text is to be bolden and I don't increment my boldCounter variable(please see my javascript code). Now if I paste my example string in contenteditable div. Following happens.




  1. If I select text "pankaj" press bold button it makes it bold and bold button does not get yellow (great, awesome).


  2. Now without selecting any text if I press bold button the button turn yellow and if I write anything after "well" word it gets written in Bold (again awesome) and after writing some bold content I again press bold button the colour of button changes to white and if I write any thing onwards everything gets written in normal font.


  3. But if I select last word of my example string and press bold button it does make the last word bold i.e "well". But the color of the button does not change to yellow. And everything gets written in Bold. So now if I want to stop bolding it I again press bold button, now color changes to yellow but fonts are normal fonts. And from here things get messed up.



I want my bold button to work like pell







javascript html execcommand






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 17:42









Mr Lister

34.3k1072113




34.3k1072113










asked Nov 20 at 15:44









pankaj roy

164




164












  • stackoverflow.com/questions/12734722/…
    – epascarello
    Nov 20 at 15:50










  • no this is not what i am trying to ask
    – pankaj roy
    Nov 20 at 15:53










  • well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
    – epascarello
    Nov 20 at 16:12












  • Then help me please . If you have your own logic that might work please suggest me .
    – pankaj roy
    Nov 20 at 16:21










  • You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
    – skyline3000
    Nov 20 at 18:47




















  • stackoverflow.com/questions/12734722/…
    – epascarello
    Nov 20 at 15:50










  • no this is not what i am trying to ask
    – pankaj roy
    Nov 20 at 15:53










  • well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
    – epascarello
    Nov 20 at 16:12












  • Then help me please . If you have your own logic that might work please suggest me .
    – pankaj roy
    Nov 20 at 16:21










  • You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
    – skyline3000
    Nov 20 at 18:47


















stackoverflow.com/questions/12734722/…
– epascarello
Nov 20 at 15:50




stackoverflow.com/questions/12734722/…
– epascarello
Nov 20 at 15:50












no this is not what i am trying to ask
– pankaj roy
Nov 20 at 15:53




no this is not what i am trying to ask
– pankaj roy
Nov 20 at 15:53












well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
– epascarello
Nov 20 at 16:12






well it sort of is. You are just holding a boolean, but that boolean has no relationship to the text you are on top of. So your logic is flawed since you have no clue if it is bold or not. You just assume it is not.
– epascarello
Nov 20 at 16:12














Then help me please . If you have your own logic that might work please suggest me .
– pankaj roy
Nov 20 at 16:21




Then help me please . If you have your own logic that might work please suggest me .
– pankaj roy
Nov 20 at 16:21












You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
– skyline3000
Nov 20 at 18:47






You can't use a simple counter for this since you can bold different parts of the text. You need to analyze if your text selection is contained in a <b> tag.
– skyline3000
Nov 20 at 18:47



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396611%2fjavascript-execcommand-bold-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53396611%2fjavascript-execcommand-bold-command%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen