Ensure JavaScriptjQuery function always runs on page load - Stack Overflow

I tried all of the alternative examples I could find and couldn't get this to work. I may have it

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">&times;</a>
    </div>
</body>
<script src=".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  
  }
});
});

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">&times;</a>
    </div>
</body>
<script src="http://code.jquery./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  
  }
});
});
Share Improve this question edited May 14, 2015 at 14:19 zrdunlap asked May 14, 2015 at 14:15 zrdunlapzrdunlap 1773 silver badges12 bronze badges 2
  • What do you mean by the values in the textarea are "passed in from another page"? Do you have a script that updates the textarea? – Katrina Commented May 14, 2015 at 14:18
  • The site I have is an internal pany search site. We give the users the option to go to a separate page an save their searches or extract some content based on the searches in specific formats. Since they ran the search on the initial page, the one I need to make the JS run gets the search passed in. Hope this makes sense. – zrdunlap Commented May 14, 2015 at 14:21
Add a ment  | 

1 Answer 1

Reset to default 5

Assigning the function to a variable lets you pass it to the textarea handler and run it on page load.

$(document).ready(function () {
    var slide = function () {
        …
    };
    $('#textarea').on('change keyup paste', slide);
    setTimeout(slide, 500);
});

Sample.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745372972a4624877.html

相关推荐

  • Ensure JavaScriptjQuery function always runs on page load - Stack Overflow

    I tried all of the alternative examples I could find and couldn't get this to work. I may have it

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信