I found that the choice can get complicated and work with errors in browsers. Throw the magic of editing documents in a browser and it will get worse!
I looked at how TinyMCE implements what I am trying to do, and used the same approach to modify jHtmlArea.
Basically, a link is created with fake href. He then finds the dom element, looking for links with this particular href. Then you can add any attributes you need and update href using the actual URL.
The solution above provided by gnarf is a great example of getting the selected node and will work for most scenarios.
Below is the code of my work:
var url = prompt("Link URL:", "http://"); if (!url) { return; } // Create a link, with a magic temp href this.ec("createLink", false, "#temp_url#"); // Find the link in the editor using the magic temp href var linkNode = $(this.editor.body).find("a[href='#temp_url#']"); linkNode.attr("rel", "external"); // Apply the actual desired url linkNode.attr("href", url);
Jason
source share