I tried all of the alternative examples I could find and couldn't get this to work. I may have it implemented incorrectly. Any help is greatly appreciated.
My issue:
I have a function that is meant to slideDown an alert if specific values in the are in the textarea.
This function works if the user types in the textarea, but the values are usually passed in from another page. How can I make it run without requiring that they interact with the text area?
Here's my example on JSFiddle: Example
And here is the HTML/JS code for easy reading:
HTML
<!doctype html>
<html>
<head>
<title>Alert Test</title>
</head>
<body>
<div class="controls">
<textarea name="q" class="span12" id="textarea" rows="5">one:</textarea>
</div>
Test alert:
<div class="alert alert-error" id="alert" name="alert" style="display: none;">
This is a test alert.
<a href="#" class="close" data-dismiss="alert">×</a>
</div>
</body>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="textAreaReader.js"></script>
</html>
JS:
$(document).ready(function(){
$('#textarea').on('change keyup paste', function() {
var lines = $('#textarea').val();
var fields = ["one", "two", "three"];
var leng;
for (var i=0; i < fields.length; i++) {
if (lines.indexOf(fields[i] + ':') !== -1){
leng = 1;
}
}
if (leng == 1){
$("#alert").slideDown("fast"); //Slide Down Effect
}else{
$("#alert").slideUp("fast"); //Slide Up Effect
}
});
});