My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
Introduction  
Updated Mar 11, 2010 by serg...@gmail.com

jwysiwyg usage and api

Introduction

This jquery plugin is an inline content editor to allow editing rich HTML content on the fly. It's an alternative to WYMeditor with much less features. With a small file size less than 26Kb total and only 18Kb of code, the main concept is to keep it simple, not all users need font coloring or create tables, just the basic.

jquery plugin

jwysiwyg is compatible with jquery 1.3.2 and later (1.4.2 tested); yo can download the last jquery version from http://jquery.com/

Quick Start

To start using jwysiwyg just add the jwysiwyg javascript and css files on head tag in your document after jquery include

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jwysiwyg/jquery.wysiwyg.js"></script>
<link rel="stylesheet" href="jwysiwyg/wysiwyg.css" type="text/css" />

Later just add the wysiwyg function over textarea element

<script type="text/javascript">
  $(function(){
      $('#wysiwyg').wysiwyg();
  });
</script>

jwysiwyg customization

For jwysiwyg customing you can use the controls variable and customize your own control buttons list.

  <script type="text/javascript">
(function($)
{
  $('#wysiwyg').wysiwyg({
    controls: {
      strikeThrough : { visible : true },
      underline     : { visible : true },
      
      separator00 : { visible : true },
      
      justifyLeft   : { visible : true },
      justifyCenter : { visible : true },
      justifyRight  : { visible : true },
      justifyFull   : { visible : true },
      
      separator01 : { visible : true },
      
      indent  : { visible : true },
      outdent : { visible : true },
      
      separator02 : { visible : true },
      
      subscript   : { visible : true },
      superscript : { visible : true },
      
      separator03 : { visible : true },
      
      undo : { visible : true },
      redo : { visible : true },
      
      separator04 : { visible : true },
      
      insertOrderedList    : { visible : true },
      insertUnorderedList  : { visible : true },
      insertHorizontalRule : { visible : true },

      separator07 : { visible : true },
      
      cut   : { visible : true },
      copy  : { visible : true },
      paste : { visible : true }
    }
  });
});
</script>

You can change your jwysiwyg configuration setting true o false to controls items

You can use the next controls:

ControlDescription
strikeThroughStrike through control Button
underlineSet ext underline
separatorXXInsert control separator you must set XX to consecutive number
justifyLeftLeft text justification
justifyCenterCenter text justification
justifyRightRight text justification
justifyFullJustify text justification
indentText indent
outdentDelete an indent level
subscriptSub Script to numbers
superscriptSuper script to numbers
undoUndo control
redoRedo control
insertOrderedListInsert an ordered numeric list
insertUnorderedListInsert an unordered list
insertHorizontalRuleInsert horizontal line (hr)
hXHeader formatting control, you must set X between 1 to 6
cutCut control button
copyCopy control button
pastePaste control button

jwysiwyg access api

You can access and modify the jwysiwyg texarea content through next api

Getting content

You can get the textarea content just using .val() method from jquery

   $('#wysiwyg').val();

Content manipulation

If you want to manipulate the jwysiwyg textarea content you can use the next methods

Insert an image

Insert an image on text indicator

$('#wysiwyg').wysiwyg('insertImage', 'http://domain.com/image.png');

Insert a link

Insert a link over selected text

$('#wysiwyg').wysiwyg('createLink', 'http://google.com/');
Comment by giovamba...@gmail.com, Mar 23, 2010

Custom Style Editor

You can custom editor style with external css file:

$('#wysiwyg').wysiwyg({css: "/mystyle.css"});

Another example:

$(function() {

    $('#wysiwyg').wysiwyg({

        controls: {

            indent : { visible : true }, outdent : { visible : true }, insertHorizontalRule : { visible : false }, removeFormat : { visible : false }, separator09 : { visible : false } 

        }, css : "/my-wysiwyg-editor.css" 

    }); 

});

And use, for example:

body {

    font-family: Monaco,Lucida Console,monospace; font-size: 13px; 

}
Comment by lightpro...@gmail.com, Mar 24, 2010

What if i want to integrate it with asp.net page, what do i have to do?

Comment by tim.stcl...@gmail.com, Mar 29, 2010

How do you get a reference to the current wysiwig editor when there are multiple elements on the page?

For instance, if I have custom button that performs a window.open to show a page that returns a HTML string from. How can I get it to execute the insertHTML method on the rte that the window.open() fired from?

Comment by gpere...@gmail.com, Apr 4, 2010

Think the textarea in the context of a form. Or fake a form. When submiting the form, ASP or PHP will receive the information. I'm doing it this way:

<textarea name="wysiwyg1" id="wysiwyg1" class="wysiwyg" ></textarea> <button class="save" onclick="save()">Save</button>

Adding a function to send the information to server side, with this value: editorInstance.val()

Comment by mako...@gmail.com, Apr 9, 2010

Is there any chance that we can make it resizeable? I tried with the Jquery Ui resizeable functionality but it failed.

Comment by akzhan.a...@gmail.com, Apr 9, 2010

makoken

resizing feature has been added.

http://akzhan.github.com/jwysiwyg/examples/04-resizable.html

Comment by gevan...@gmail.com, Apr 12, 2010

How does one "un" wysiwyg a textarea? Is this even possible i.e. I would like to make the textarea a wysiwyg on entry and then return it to a normal textarea on exit.

Comment by akzhan.a...@gmail.com, Apr 12, 2010

gevanzyl

When You using latest jwysiwyg from http://github.com/akzhan/jwysiwyg

Simply do $('#editor').wysiwyg('destroy');

Comment by sa...@designergruppe.com, Apr 19, 2010

gevanzyl

Why bother resetting them when exiting!?

Comment by miguelde...@gmail.com, Apr 19, 2010

How does one repopulates the wysiwyg? Eg: on ajax post, repopulate it: I tried $('#wysiwyg').wysiwyg('setContent',html);

but this repopulates without applying the html formatting, so one will get: b>xpto</b> instead of a bold "xpto" string.

Comment by akzhan.a...@gmail.com, Apr 20, 2010

migueldealmeida

Retry it with http://github.com/akzhan/jwysiwyg

If error persists, attach page with broken behavior to be fixed.

Comment by ke...@ta1.com, May 7, 2010

I tried a simple test with what was above http://akzhan.github.com/jwysiwyg/examples/04-resizable.html and ran into some problems with bulleting.

So I created some random sentences. Then enter x2 then made a list of 3 items. When I clicked bullet, the entire contents were indented as 1 bullet.

I could NOT get the list bulleted no matter what. I could Not get anything bulleted except line 1 of text which grabbed everything in the content.

I then tried adding a hard rule. Then I was able to create a bullet list after, but if I deleted the hard rule and the extra space, the bulleting went away and i Was left with the original problem - the whole glob was a bullet.

Any thoughts - I really like the simplicity, but the basics have to work for me. Thanks

Comment by ke...@ta1.com, May 7, 2010

Also xhtml compliant - I am not well versed, but shouldn't <BR> be <br /> - just wondering...

Comment by weiqiy...@gmail.com, May 10, 2010

i just set the textarea width property, and wish it auto break line when my input meets the right edge, but i failed!

Comment by alanbend...@gmail.com, May 16, 2010

I have made a i18n version of this editor, but can't seem to find a contact so I can send the zip file. Anybody? You can reach me on ardaviesATtiscaliDOTcoDOTuk if you're interested.

Comment by ryne.dio...@gmail.com, May 17, 2010

Is there way to disable the wysiwyg area? That is, be able to see the contents correctly formatted, but unable to edit?

Comment by akzhan.a...@gmail.com, May 18, 2010

$('#your-ed').replaceWith($('#your-ed').wysiwyg('getContent'));

Comment by nicowest...@gmail.com, May 18, 2010

This seems to have some problems when I browse to a site using it on an iPhone with Safari. Text is inserted as all caps and focusing is really hard. Has anyone come across this? The site I'm testing on is: http://www.bitsdujour.com

Comment by michael.t.bryan@gmail.com, May 20, 2010

@gevanzyl

you might be able to use a function that adds a class or ID to the textarea on focus then removes it onblur. I haven't tried it myself but was considering it. If I get something worked out I'll post it.

Comment by nalvare...@gmail.com, May 27, 2010

insert text in wysiwyg?

$('#wysiwyg').val('MyVal?'); ????

Comment by timbro...@gmail.com, May 28, 2010

Hello,

I have three textareas on a page and have given each a unique id... when calling the function I reference each id individually $('#id1').wysiwyg(); the first textarea is rendered as the wysiwig, however, the others are not???? Any reason why this may be falling over.

Comment by daniel.i...@gmail.com, Jun 1, 2010

sadasdasdadasd

Comment by andy%txm...@gtempaccount.com, Jun 3, 2010

Paste from Word?

Comment by rifatero...@gmail.com, Jun 3, 2010

how can i open popup then insert textbox value on the popup window to the opener wysiwyg ? Thanks

Comment by animator...@gmail.com, Jun 5, 2010

New to php/js, How do I send jwysiwyg's value to php script by pressing "submit" button of a form. This works in js <input type="button" value="Show HTML" onclick="alert($('#wysiwyg').val());" />

Comment by seanmich...@gmail.com, Jun 9, 2010

I would like to embed raw HTML using jwysiwyg (so that I can add a gist inline with my blog post). Is this possible?

Thanks.

Comment by 0z0n...@gmail.com, Jun 26, 2010

resolved a problem with a border when click on button on a toolbar via CSS: img, a:focus{ outline: none; }

Comment by akzhan.a...@gmail.com, Jun 27, 2010

0z0n, please use latest 0.91 version from http://github.com/akzhan/jwysiwyg/downloads

There are many bug fixes and improvements.

Comment by pernica....@gmail.com, Jul 5, 2010

Hello.

Can you add button for pre/code tag?

Thanks.

Comment by fraticiu...@gmail.com, Jul 9, 2010

Hello, great plugin super easy to setup, good job.

I was wandering is there is any way to simulate the focus event (when the user clicks the editor or "TABs" through the form). I just need a simple background color change, or an addClass.

Thanks.

Comment by bernab...@gmail.com, Jul 14, 2010

how to do a button for text color ??

Comment by morphi...@gmail.com, Jul 16, 2010

Was getting an error in ie8

Message: Could not set the cols property. Invalid property value. Enter a value greater than zero. Line: 657 Char: 13 Code: 0 URI: http://localhost:21329/Scripts/jwysiwyg/jquery.wysiwyg.js

change both:

if (newX === 0 && element.cols) {
newX = (element.cols 8) + 21; element.cols = 1;
} if (newY === 0 && element.rows) {
newY = (element.rows 16) + 16; element.rows = 1;
}

Now dont get any error. Should this "fix" be in the source or is there a better method i should have done.

Comment by Jeremiah...@gmail.com, Jul 27, 2010

Just want to ask... can I impliment "Spell as you type jquery plugin here"?

here is the link = http://www.opencomponents.com/spellayt/demo/

$('#txtTextarea').each(function(input) {

$.fn.spellayt.checkSpelling(this);
});

Comment by chris.j....@gmail.com, Jul 27, 2010

how can I attach an onChange event, so I can capture the text as it is being written? or is there another way of doing this?

Comment by giovanin...@gmail.com, Jul 27, 2010

i'm trying to insert youtube videos, i have accomplish show the dialog box asking for the video URL and insert the embed code youtube gives but i can't make it work, it's like it wasn't rendered or something. Could anyone help me with that?

Comment by s1kb0y.s...@gmail.com, Sep 1, 2010

$('#my-editor').wysiwyg('clear',""); $('#my-editor').wysiwyg('setContent',"content to insert"); $('#my-editor').wysiwyg('insertHtml',"append content to editor");

if you read through the js file you would find these commands and be able to update modify wysiwyg in realtime via code

great jQuery tool, thanks

Comment by stusmit...@gmail.com, Sep 10, 2010

What would be the method to replace DIV tags with P tags for line breaks and paragraphs. Right now when we type text and line break it inserts div tags which is breaking our template design. Or if someone pastes text in to use p tags for paragraph breaks by default.

Comment by rexo...@gmail.com, Sep 13, 2010

How do you get multiple instances of the editor to appear on a single page? I've tried all kinds of things, but nothing's working.

Comment by ysfgilb...@gmail.com, Sep 14, 2010

give every textarea a different id, and call $('#area-id').wysiwyg(); for every textarea in document ready area.

Comment by rexo...@gmail.com, Sep 14, 2010

Thanks, @i...@youssef.de - totally an obvious solution!!! Guess I shouldn't be trying to program late at night! I ended up just calling a class, $('.wysiwyg').wysiwyg(); and applying the class to the different textarea's I want to wysiwyg to accompany. Thanks again!

Comment by jando...@gmail.com, Sep 16, 2010

What about image upload from disk?

Comment by hein.oos...@gmail.com, Sep 22, 2010

How can i use this code with the wysiwyg - now:

jQ(document).ready(function() {

jQ("#textbox").keyup(function () { alert(jQ(this).val()); });
});

In other words how can i capture the keyup on the wysiwyg control?

Comment by scott.stafford, Oct 11, 2010

FYI to all: This project has been moved to github and is now here: http://github.com/akzhan/jwysiwyg

Comment by bbelekk...@gmail.com, Dec 1, 2010

How count words in content

Comment by cosmin.c...@gmail.com, Mar 3, 2011

Hello!

Is there any way of applying a default class to all the uls in the entered text?

Thank you.

Comment by doanhuut...@gmail.com, Mar 18, 2011

Hi, I don't setting language for it. Can you help me? thanks

Comment by vysha...@gmail.com, Apr 13, 2011

I'm sorry, but how can i send what i have typed in the field to the PHP script?

Comment by nestors...@gmail.com, May 13, 2011

Using Chrome it insters <div> instead of <p> and breaks my design. How to make it insert <p> instead of <div>?

Comment by marc.c.j...@gmail.com, Jun 14, 2011

Hi, I've been stopped cold by something really simple: referencing the css style sheet for the textarea in jwysywig. nothing I'm doing is working? What is the path relative to? Any particular reason you don't make an option to just suck in the css associated with the original textarea?

Comment by kendo...@gmail.com, Jul 13, 2011

why cant i change the width of the textarea?... its stuck... i tried within css of the textarea and i tried formWidth in the javascript... wow... lame.

Comment by shahidkh...@gmail.com, Oct 5, 2011

how can i get response from editor textarea iframe using onblur function plz do help if you have any solution

Comment by kunterku...@gmail.com, Nov 21, 2011

is that all, what about setting value, i'm not mention about initialContent. for example if i want to set it's value when i click a button. insertHTML as it says inserting on the old value.

Comment by kunterku...@gmail.com, Nov 21, 2011

oh sorry i found the answer on top:) thanks s1kb0y.s...@gmail.com

Comment by Arsen...@gmail.com, Jan 19, 2012

Hi I found,that if you do some text formatting and after remove all text,the formatting still remains for example like this : <i><br><strike></strike></i>. As you see no text are there but this prevent my validation for work correctly. Any help?

Comment by v...@neekman.com, Apr 16, 2012

Would you be able to disable image upload and only allow image links?

Comment by hhgtg...@gmail.com, Apr 28, 2012

I am using this in asp.net page. I first initialise using

$('#jwysiwyg').wysiwyg({
resizeOptions: {}
})
where #jwysiwyg is the text area.then I initialize a dialogue box from a div_tooltip surrounding this textarea. On a buttons click I want to show the dialogue with editor in it and content of div_1 so I do
$('#jwysiwyg').wysiwyg('insertHtml', $('#div_1').html()).wysiwyg("destroy");
But I dont see the content and the text area of the editor is disabled.


Sign in to add a comment
Powered by Google Project Hosting