The following fiddle allows text to be pasted into a <textarea>
and generated into paragraphs on button click.
Is it possible, within the following code below, to create two dropdown lists <select></select>
, one that changes the color of the paragraphs <p>
to any color the user selects on click and the other to change the size of the text dynamically?
Attached is the Fiddle. It would be extremely helpful if the fiddle could please be updated, as I am still new to coding.
HTML:
<div>
<div id="text_land" style="border:1px solid #ccc; padding:25px; margin-bottom:30px;">xzxz</div>
<textarea style="width:95%;"></textarea>
<button>Go</button>
Javascript:
$(function () {
$('button').on('click', function () {
var theText = $('textarea').val();
var i = 200;
while (theText.length > 200) {
console.log('looping');
while (theText.charAt(i) !== '.') {
i++;
}
console.log(i);
$("#text_land").append("<p>" + theText.substring(0, i+1) + "</p>");
theText = theText.substring(i+1);
i = 200;
}
$('#text_land').append("<p>" + theText + "</p>");
})
})
Thank You!
While you've already accepted an answer, I thought I'd offer a slight alternative; which is a little more concise and, hopefully, obvious as to how to extend further to support other css properties:
JS Fiddle demo.
The advantage of this approach is that one event-handler can act as the event handler for all the
<select>
elements, so long as the appropriate values are given in the<option>
tags, and the appropriate CSS property is held in thedata-property
attribute of the<select>
element itself. This avoids the need to write out event-handlers for each individual<select>
element that updates a different property-value.References:
Array.prototype.join()
.Element.dataset
.String.prototype.split()
.appendTo()
.css()
.on()
.prop()
.val()
.Following is the Updated Fiddle.
Code
create css classes eg: .red,.blue etc
.red { color : red; }
<p>
tags in the block and add the required class to the<p>
tags (<p class="red">
).Repeat the same for changing the font size by creating css class of required font size