jquery - Execute Javascript if element with specific CSS class exists - Stack Overflow

I have a page that displays an error div with a class of 'validation-errors' if the user ente

I have a page that displays an error div with a class of 'validation-errors' if the user enters an incorrect value into a textbox.

Is there a way to execute a javascript function (it adds a CSS property to a button) when the error div is displayed? The function is currently called in window.onload.

Is there a way to execute the javascript function if window has changed / element added?

I have a page that displays an error div with a class of 'validation-errors' if the user enters an incorrect value into a textbox.

Is there a way to execute a javascript function (it adds a CSS property to a button) when the error div is displayed? The function is currently called in window.onload.

Is there a way to execute the javascript function if window has changed / element added?

Share edited Sep 19, 2012 at 12:18 Danil Speransky 30.5k6 gold badges69 silver badges78 bronze badges asked Sep 19, 2012 at 11:56 TheomaxTheomax 6,81015 gold badges55 silver badges73 bronze badges 5
  • 1 Have you considered using JQuery to do this? – Liam Commented Sep 19, 2012 at 11:59
  • Yes, but I'm not sure how I would do that. It's an MVC website, and I don't actually know/understand how the validation message is being displayed - if I did, I could add a call to the function to apply the CSS to the button when the error div/message is displayed. The button basically needs to be updated when this error message is displayed. – Theomax Commented Sep 19, 2012 at 12:02
  • Here's a basic introduction to JQuery: docs.jquery./How_jQuery_Works. It's basically a JavaScript library that allows you to, among other things, select elements based on the class, id, etc. and trigger events. – Liam Commented Sep 19, 2012 at 12:05
  • @Liam - Thanks, I can see already it's better than Javascript – Theomax Commented Sep 19, 2012 at 12:08
  • @aspdotnetuser If you just write the CSS-rules in your stylesheet they will apply even to nodes added with AJAX. – 11684 Commented Sep 20, 2012 at 19:59
Add a ment  | 

3 Answers 3

Reset to default 4
$('.validation-errors').bind('DOMSubtreeModified', function () {
  if ($('.validation-errors').length) { 
    // code
  }
});

If you have jQuery on the page:

if ($('.validation-errors').length){ /*your code here */ }

in pure JS it would be something like this:

if (document.getElementsByClassName("validation-errors").length){ /*your code here */ }

if you can use jQuery, you can do

$(".validation-errors").live(function() {/* do whatever you want when your error div exists */ });

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信