RichEditor use execCommand('bold') twice append extra <span> label - javascript

I use execCommand('bold') to make the following text bold, but when I try it again to disable bold, then add other label like 'h' or something, It add extra label link this:
<h1><span style="font-weight: normal;">111</span></h1>
I wonder how to avoid this?

It should works
First of all if you really doing it correctly, it won't happen like that.
Please see the sample below, it should works.
document.designMode = "on";
function myFunction(event) {
if (event.keyCode == 16) {
// Press shift btn exec cmd for bold trigger
document.execCommand("bold");
//check in alert box when triggered
//alert( document.getElementById("thebody").innerHTML );
//check the updated code in console once triggered
console.log( document.getElementById("thebody").innerHTML );
}
}
<!DOCTYPE html>
<html>
<body onkeydown="myFunction(event)">
<div id="thebody">
<h1>Exec execCommand("Bold")</h1>
<p>Try to exec by pressing shift btn once highlighed</p>
<h2>Again execCommand("Bold")</h2>
<p>Select some text in this page, and press the SHIFT button to make the selected text toggle between bold and normal.</p>
</div>
<div id="preview">
</div>
</body>
</html>
Note/Advise:
However, if your problem persists in your version of code, please share with me the function that trigger this exec Cmd.

javascript code is just like this:
Editor.setHeading = function(heading) {
if(heading == 0){
document.execCommand('formatBlock', false, '<div>')
} else {
document.execCommand('formatBlock', false, '<h'+heading+'>');
}
}
Editor.setBold = function() {
document.execCommand('bold', false, null);
}
html is:
<body>
<div id="editor" contenteditable="true"></div>
<script type="text/javascript" src="editor.js"></script>
</body>

Related

Creating a "Copy button"

Well, as I was searching on the internet for some basic codes to examine - I found this one. A simple code which is supposed to copy the selected text. As i am a complete newbie in JS, I check the meaning of the methods that I didn't understand - and rewrited the code, as i make a few adjustments.
And still the code is not working and If someone can explain - this part ""copyit(this.form.select1)"" - Even though I kind of understand "this" - i am not able to understand what is doind here
function copyit(theField) {
var selectedText = document.getSelection();
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>
<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to copy the highlighted text" name="btnCopy">
<p>
<textarea name="select1" rows="4" cols="45"></textarea>
</div>
</form>
This is the original code - and it is not working either
<SCRIPT LANGUAGE="JavaScript">
function copyit(theField) {
var selectedText = document.selection;
if (selectedText.type == 'Text') {
var newRange = selectedText.createRange();
theField.focus();
theField.value = newRange.text;
} else {
alert('select a text in the page and then press this button');
}
}
</script>
And in the body of your web page, add the following where you want the text to appear:
<form name="it">
<div align="center">
<input onclick="copyit(this.form.select1)" type="button" value="Press to copy the highlighted text" name="btnCopy">
<p>
<textarea name="select1" rows="4" cols="45"></textarea>
</div>
</form>
onclick="copyit(this.form.select1)"
executes the copyit() function and passes a variable which is later named theField. The variable that is passed is this.form.select1 which is a textarea with ID select1 which is located in the same form as the input you're clicking hence the this.form.
As to why your code isn't working - you should include here the original code before your adjustments. You probably deleted/changed something you shouldn't have.
I'm not sure what you're asking. Are you asking to, when someone clicks on any button/div, it copies a text you want for his clipboard? If no, ignore my comment, if yes, i'll explain:
First place, where should an user click?
<a class="btn" CopydivFunction(#text)">CLICK ME TO Hello.</a>
Now, add the function with JS.
function copyToClipboard(element) {
var $temp = $("<input>");
$("body").append($temp);
$temp.val($(element).text()).select();
document.execCommand("copy");
$temp.remove();
}
Now, place the text you want somebody to copy (hide it):
<h1 id="text" class="hidden">some text. This part won't be seen because of the hidden class, and this is the text that will be copied to your clipboard.</h1>
Place display:none on css:
#text{
display:none;
}
I think you have to add that, so nobody sees it.
And that should be it, click the <a> and you get the text in the h1#text

Check when an element has been selected/tabbed onto

I have an HTML page where I have a few divs with contenteditable="true". I also set the tabindex sequentially so when the user hits tab he or she will go through all of the editable divs. However, right now I have "Insert notes here" written inside each of the divs (see below).
<div contenteditable="true" tabindex='1'>Insert notes here<div>
<div contenteditable="true" tabindex='2'>Insert notes here<div>
<div contenteditable="true" tabindex='3'>Insert notes here<div>
I'm trying to get rid of the "Insert notes here" text when the user tabs to the div. Right now, I'm able to get rid of the text if they click on it with the following jQuery:
function selectText(containerid) {
if(document.getElementById(containerid).innerHTML == "Insert notes here") {
document.getElementById(containerid).innerHTML = "";
}
}
Is there a way to achieve the same effect but also when the user uses tab?
sounds like you are looking for the onfocus event
http://www.w3schools.com/jsref/event_onfocus.asp
In jquery:
$("#fieldId").focus(function() {
//your code here
});
Try this working snippet
$(document).on('keyup, focus', '.editor', function(e) {
this.innerHTML = "";
//detect 'tab' key
if (e.keyCode == 9) {
//add tab
this.innerHTML = "";
//prevent focusing on next element
e.preventDefault()
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div contenteditable="true" class="editor" tabindex='1'>1
</div>
<div contenteditable="true" class="editor" tabindex='2'>2
</div>
<div contenteditable="true" class="editor" tabindex='3'>3
</div>

Can't change a paragraph text in javascript through DOM

it changes for about a second and returns to the previous text.The "Loading..." line has to change into "hi, Please click the next text box to see more instructions!".
I have tried it latest chrome and Edge browsers.
function greetMe() {
var yourName = document.getElementById("textbox").value;
info1 = "hi, Please click the next text box to see more instructions!"
document.getElementById("textToChange").innerHTML = info1
}
#myForm {
float: left;
width: 30%
}
#myformInfo {
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>HEllO ThERE!</h1>
<div id="myForm"><form >
<input id="textbox" placeholder="Your name">
<button onclick="greetMe()">click!</button>
<br><br>
<input id="">
</div></form>
<div id="myFormSteps">
<p id="textToChange">
<script>var info1 = "Loading..."
document.write(info1)
</script>
</p>
</div>
</body>
</html>
It's probably because you haven't set the type attribute for your button. A button's default type is submit. Try adding the attribute type="button" to your <button>.
When you click the button your form is submitting and the page is reloading - that's why it returning to its initial state. To stop this happening pass in event as a parameter to the function and then use that argument in the function with preventDefault():
HTML
<button onclick="greetMe(event);">click!</button>
JS
function greetMe(e) {
e.preventDefault();
// ...
}
As an aside it's better is to remove your inline JS and use an event listener instead.
var button = document.querySelector('button');
button.addEventListener('click', greetMe, false);

javascript - one image,two actions

I am looking for javascript command that would do the following:
Click on image -> open spoiler
Click on image again -> hide spoiler
Here is what I got so far:
javascript in my html
<script>
function myFunction() {
document.getElementById("prvy").innerHTML = document.getElementById('spoiler_id').style.display='';}
</script>
Spoiler
<a id="show_id"
onclick="document.getElementById('spoiler_id').style.display=''; document.getElementById('show_id').style.display='none';"
class="link"></a><span id="spoiler_id"
style="display: none">[Show]<button onclick="document.getElementById('spoiler_id').style.display='none';
document.getElementById('show_id').style.display='';"
class="link">[Hide]</button>
<br><h1 id="bz">Heading</h1><br><br><p>text</p></span>
And my button:
<div id="prvy" onclick="myFunction()"></div>
What I managed to do, is to click on a image, wich will open spoiler. Hovewer, I've been unable to do the second part, onclick again it will close the spoiler.
I also did serach for solution alredy, nothing worked for me, not even this: Link
I also tired if{} else{} statement but didn't work for me either.
Help would be really appreciated, as I am getting desperate on this one.
You can use jQuery .toggle() to toggle show/hide
$("#prvy").click(function() {
$("#spoiler_id").toggle();
});
Note : You need to include jQuery in your document as
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Working snippet :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a id="show_id"
onclick="document.getElementById('spoiler_id').style.display=''; document.getElementById('show_id').style.display='none';"
class="link"></a><span id="spoiler_id"
style="display: none">[Show]<button onclick="document.getElementById('spoiler_id').style.display='none';
document.getElementById('show_id').style.display='';"
class="link">[Hide]</button>
<br><h1 id="bz">Heading</h1><br><br><p>text</p></span>
<div id="prvy" onclick="myFunction()">button</div>
<script>
$("#prvy").click(function() {
$("#spoiler_id").toggle();
});
</script>
In the JavaScript where you click the button use the simple jQuery function toggle.
$('#spoiler_id').toggle();
Toggle will hide the element selected if it is currently shown or display the element if it is currently hidden.
you would need some state that flips when the function is called.
like this.
<script>
var state = false;
function myFunction() {
state = !state;
if(state){
//do something
}else{
//do something else
}
}
</script>
Is that all of your code, it would be easier for you and less confusing too if you just gave the buttons an on click function and then called that function in your js.
Can I see all of your html
I am giving an example to concerned question using javascript.
HTML:
<script type="text/javascript">
var permit = 'true';
function showhide() {
var getcont = document.getElementsByClassName('hidshowcont');
if (permit === 'true') {
permit = 'false';
getcont[0].style.display = 'block';
}
else {
permit = 'true';
getcont[0].style.display = 'none';
}
}
</script>
<style type="text/css">
.hidshowcont{
height: 200px;
width: 300px;
border: 1px solid #333333;
display: none;
}
</style>
<img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR1cSDTn18ufwjuMihttTvCPJOnFY-4hxbPcaOVd87nSPaQakbP9IERaQ" />
<br />
<br />
<div class="hidshowcont">
This is an example of hide and show the container by clicking of an image.
</div>
This will help u much

Changing the tooltip of button dynamically in html

I am very much new to html, css, javascript, jQuery. I want to show tooltip depending the button. Button while showing Play icon it should show 'Run' as tooltip and when Stop icon is showing it should show 'Stop' as tool tip. This code is part of HTML template
<div class='btn-group'>
<button class='btn btn-inverse playstop' rel='tooltip'
data-original-title='Run><i class='icon-play'></i></button>
</div>
In the script for one condition i have
$(".icon-stop", $this.element).attr("data-original-title", "Run");
$(".icon-stop", $this.element).removeClass("icon-stop").addClass("icon-play");
and for other condition i have
$(".icon-play", $this.element).attr("title", "Stop");
$(".icon-play", $this.element).removeClass("icon-play").addClass("icon-stop");
I have edited to show a full html of a button which changes its text when clicked from Stopped to Running. The tooltip displayed is also changed on click
<html>
<head>
<meta charset="ISO-8859-1">
<title>TestingStuff</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
</head>
<script>
$(document).ready(function(){
$("#aBtn").click(function(){
if($("#aBtn").html() == "Stopped"){
$("#aBtn").html("Running");
$("#aBtn").attr("title","Running");
}
else{
$("#aBtn").html("Stopped");
$("#aBtn").attr("title","Stopped");
}
});
});
</script>
<body>
<div>
<button id="aBtn" title="Stopped">Stopped</button>
</div>
</body>
</html>
I'm not really sure why you need a tooltip that displays the text on a button, but, here's an inelegant, brute force FIDDLE for you to consider. You can play around with the text anyway you want.
JS
$('.playbutton').html('STOP');
$('.tooltip').html('STOP');
$('.playbutton').on('click', function(){
if ( $('.playbutton').html() == "PLAY" )
{
$('.playbutton').html('STOP');
$('.tooltip').html('STOP');
}
else
{
$('.playbutton').html('PLAY');
$('.tooltip').html('PLAY');
}
});
$('.playbutton').hover(
function(){
$('.tooltip').css('display', 'block');
},
function(){
$('.tooltip').css('display', 'none');
}
);

Categories