tags:

views:

76

answers:

1

Hi all,

I have one question. I try to develop a little WYSIWYG editor.

I would like create a <h1> button which allows me to generate a <h1> title (by clicking on button after text selection in <p> element).

I obtain the selection with window.getSelection().... But now, I would like to put my append("<h1>my text selected</h1>") just before the <p>. It’s my problem, because my <p> elements don’t have id or class. So do you know a means to put my append just before the <p> where my text has been selected?

I don’t know if with my bad English you’ll understand me!

Thanks very much for your help.

A: 

Have you tried using something like jQuery?

$("#yourButton").click(
    function(){
      $('<h1>'+window.getSelection()+'</h1>').insertBefore("???");
    }
);

Just replace "???" with the location of your <p> element. How you would find that location depends how you wrote your HTML. Feel free to post your HTML for more assistance.

Jeremy
Thanks for your assistance. Please find my HTML :<div id="my_editeur" contenteditable="true"><p>Voici mon premier paragraphe.</p><p>Voici mon second paragraphe.</p><p>Voici mon troisième paragraphe.</p><p>Voici mon quatrième paragraphe.</p></div>In this case for example I would like to transform "Voici mon troisième" in h1 title, and to insert it before the third <p>
sanceray3
You're missing a step. So far I have:1. Select text from `p`2. ?3. Place text in new `h1` above `p`
Jeremy
1. I select my text from '<p>'2. I click on button $('#btn_h1'). I use getSelected() function to obtain my selection ('var selection = getSelected();')3. I would like to place my selection in new '<h1>' above the '<p>' where I have select the text?
sanceray3