Jquery: set style part of text in input text - javascript

I want to create some text based on user input, and when user enter some text, the first word will automatically set backgroundcolor, but the other words still remain same (no background color), can I do this ?
Thanks

WYSIWYG editor will be the best choice for previewing text and editing at the same time.
Some popular ones are
TinyMCE
STEditor
FCKeditor

You could do it with an iFrame if you turn designMode on, add an event listener for keypress then wrap the first word in the iFrames body with <span style='background-color: red'></span>. You would then have to have a hidden form field that mirrored the data that is typed into the iFrame (removing the span tag).
If you want to go this way let me know if you need any more help.

Related

How do I highlight new text within a input field in html

Within Servicenow portals I am trying to create a input text field that has a few requirements when the user tries to make changes to the text. Within the form if the user wants to change the text that has already been added the new input text will have to be highlighted. Only the new changes can be highlighted. The previous text will stay the same without any highlights. I have looked into the html tag but that highlights the entire text field and also tried using a 'focus' within the css tag name.
HTML:
<input type="text" value={{data.text2}}>
CSS:
input:focus::first-line { background-color: #fff2ac;
}
You could use Javascript to position a dynamically generated span with the highlighting, on top of the input ... and then update that span's text whenever the underlying input changes.
See the last option in the top answer here for more details: How to highlight text inside an input field?.
It even mentions a jQuery plug-in with this effect that you could use, or examine the source of: https://github.com/garysieling/jquery-highlighttextarea.

Restrict selection between paragraph

I´m working in a web application using angular, one of the sections is document section where the user is gonna be able to select the text and highlight the text, i´m using window.getSelection() to extract the selected text and appended in to a <mark> tag to highlight the text, but one of the requirements is that the is user is not gonna be able to select text between paragraphs for example:
In the image there is a selection between paragraph is there any way to avoid this, or stop the selection when the user jump from one paragraph another.
Ok i found small hack of how to do it, i attach some properties to the paragraph tag here i put the code.
<p oncut="return false" onpaste="return false"
onkeydown="if(event.metaKey) return true; return false;" contenteditable="true"
spellcheck="false" autocorrect="off">Text</p>
Using this the selection is not possible to select between elements.
Maybe you could calculate a substring before attaching it to the mark.
Using a regex like /\r?\n/ or something similar to keep only the first chunk of the copied text in both *NIX and Windows systems (user-agnostic, to some extent).

hyperlink and bold word text display after form submission with innerhtml with javascript

i display a block of text(based on user input) after form submission using innerHTML. i just wanted to know how to make a bold hyperlink within the text block. The text is displayed in a hidden panel below the form.
Many Thanks
I'm not sure I fully understood you question, but...
you can use the old HTML tags (like <b> or <i>) directly into the innerHTML = ""; without them being stripped.

HTML input text in 2 colors

I want to show some text in red color and the remaining in black inside one html textbox after the on change event or mouse leave event.
Is this possible? if yes how?
I don't think you can achieve specifically what you're asking for. You can apply CSS to the default value of a text box, but to change the colour of only some of the words within it would require you to use a <span>, which you can't within a text box value (i.e. you can type <span>, of course, but not apply CSS to it).
If you're happy to have the text within some other element as you imply in a later comment, then that's perfectly doable with very simple CSS using spans within <p> or <div> elements, as evidenced in the below jsFiddle:
http://jsfiddle.net/2DpSX/
Finally I could do this by creating a editable <div>
http://jsfiddle.net/EP2dc/
Thanx everyone for the guidance.

How to move placeholder text along with the content of a textbox in HTML?

Suppose this is my textbox:
<input type="text" placeholder="%" />
And a user is supposed to enter a percentage inside, but without the % sign, only the numbers (e.g. 67 in 67%). But I want them to still remember that this is a text box in which you insert a percentage.
So how can I move the placeholder along with the text, make it unable to be deleted, always after the text?
And I do remember seeing it somewhere too, unless I got my facts wrong.
A way to do this would be to have an additional element overlaying the input element and moving the overlayed element as the user types.
But, I think a better UX experience would be to have the element as an add-on appended to the input field, as show in twitter bootstrap. See the "extending form controls" settings:
http://twitter.github.com/bootstrap/base-css.html#forms
You could simulate an input and change the width of the real input using javascript. (The trick is to use some invisible element to catch the needed width)
Exemple using JQuery: http://jsfiddle.net/Vu7hN/
$input.on('change keypress paste focus textInput input', function(){
testWidth.text($input.val());
$input.width(testWidth.width());
});

Categories