You can use length to see if your selector matched anything.
if ($('#MyId').length) {
// do your stuff
}
More Related Contents:
- How to negate code in “if” statement block in JavaScript -JQuery like ‘if not then..’
- Why is my javascript not working
- event.preventDefault() vs. return false
- Inserting a text where cursor is using Javascript/jquery
- JavaScript: filter() for Objects
- jQuery click / toggle between two functions
- jQuery.parseJSON throws “Invalid JSON” error due to escaped single quote in JSON
- Force browser to download image files on click
- Event detect when css property changed using Jquery
- Using jQuery to replace one tag with another
- jQuery: load txt file and insert into div
- How to copy text from a div to clipboard
- How to send data in request body with a GET when using jQuery $.ajax()
- How can I detect with JavaScript/jQuery if the user is currently active on the page?
- Setting CSS value limits of the window scrolling animation
- Focus next input once reaching maxlength value
- capturing f5 keypress event in javascript using window.event.keyCode in window.onbeforeunload event is always 0 and not 116
- Preload mp3 file in queue to avoid any delay in playing the next file in queue
- How to know if JQuery has finished loading [duplicate]
- set option “selected” attribute from dynamic created option
- Change url when manually scrolled to an anchor?
- Can’t trigger click with jQuery in a Chrome extension
- How to add DOM element script to head section?
- Convert js Array() to JSon object for use with JQuery .ajax
- getSelection() not working in IE
- selectionStart-End with textareas
- How to send parameter json array when modal open?
- Flask Dynamic data update without reload page
- Convert css width string to regular number
- How to simulate TAB on ENTER keypress in javascript or jQuery