1

I've searched and searched and can't seem to find an answer to this so please forgive me if I'm asking a question that's already been answered.

I use Tampermonkey for Chrome. It's the Chrome version of Greasemonkey. I'm a frequent user of a forum at http://forum.xda-developers.com. Whenever I go to post a reply on that forum I like to change the font. The reply editor is somewhat like the editor here on Stackoverflow-- a code editor. So when you change the font what it does is wrap font tags around your text in your editor's text field like this example below:

[FONT="Arial"]This is a reply.[/FONT]

Now what I'd like to do is use Javascript or jQuery to automatically prepend [FONT="Arial"] and append [/FONT] to the editor's text input field upon page load.

So, that being said, what Javascript or jQuery would I use to automatically prepend and append that text upon page load?

2 Answers 2

1

is something like this what you want?

$(document).ready(function() {
  var originalData = $('textarea').val();
  $('textarea').val('[FONT="Arial"]' + originalData + '[/FONT]');
});
/*unnecessary css, just for asthetics of code snippet*/
textarea{
  width: 300px;
  height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>Some text that is already in here</textarea>

UPDATE WITH VANILLA JAVASCRIPT:

document.getElementById('vB_Editor_001_popup_fontname').setAttribute("onclick", "myWrappingFunction()");

function myWrappingFunction() {
  var originalData = document.getElementById('vB_Editor_001_popup_fontname').value;
  document.getElementById('vB_Editor_001_popup_fontname').value = '[FONT="Arial"]' + originalData + '[/FONT]';                        
}
/*unnecessary css, just for asthetics of code snippet*/
textarea{
  width: 300px;
  height: 100px;
}
<textarea id="vB_Editor_001_popup_fontname">I am the nextarea with id vB_Editor_001_popup_fontname</textarea>

Sign up to request clarification or add additional context in comments.

13 Comments

Yes but that seems to replace all the text in the text area. I'd like to leave the text that's already present in the text area and just prepend and append that text around the existing text. Does that make sense?
Yes!! That did it! Thank you so much!
Do you know how to change from $(document).ready(function() { to make the javascript only happen when clicking an element like this? id="vB_Editor_001_popup_fontname"
@MisterPyrrhuloxia: yup, updated answer above with just vanilla js
So I think that I don't want the prepend/append event to happen till after I click on the element id "vB_Editor_001_popup_fontname". I used your vanilla javascript above and it didn't seem to do that. Instead, no prepending or appending happens when I click on the vB_Editor_001_popup_fontname element.
|
0

you can inject jquery to the page (if it's not already part of the page) and use

$(document).ready(function(){
    $('#textAreaID').val('[FONT="Arial"]This is a reply.[/FONT]');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.