Clicking a <span> tag to run JQuery javascript function

Keith D Kaiser

I create a span using PHP like this.

if ($subnetkey  == 1 ) { echo ("<span class='subnetkey'>S/N of: $subnetnum</span>&nbsp;&nbsp;");}

It works, and shows the correct data on screen. Additionally if I look at it using 'Inspect Element' its properly formatted.

<span class="subnetkey">S/N of: 780</span>

I have this script at the top of the page. I've also tried it at the bottom.

<script>    
$(document).ready(function(){
    $(".subnetkey").click(function() {
        alert("subnet click mode");
    });
});
</script>

When I click the span, nothing happens. I get no errors, and of course I don't see the alert fire.

It seems like this is a timing issue between building the page dynamically and using the page. But in case thats not it, what can I do to make the function fire?

iiirxs

JQuery Event Methods like click(), dblclick(), mouseenter() etc. work only for elements already created when DOM is rendered. For dynamically created elements you use on() method with the below syntax (see previous post):

$(staticAncestors).on(eventName, dynamicChild, function() {});

Since it is a dynamically created element your code won't work. Try:

$(document).on('click', '.subnetkey', function() {
    alert("subnet click mode");
});

jQuery is only aware of the elements in the page at the time it runs, so new elements added to the DOM are unrecognized by jQuery. To combat the problem use event delegation, bubbling events from newly added items up to a point in the DOM which was there when jQuery ran on page load. Many people use document as the place to catch the bubbled event, but it isn't necessary to go all the way up the DOM tree. Ideally you should delegate to the nearest parent existing at the time of page load.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jquery append function with span tag

Why delete function is not calling on clicking span tag appended in div

remove() function of jQuery not working not removing span tag

Add span tag with javascript function to html table

jQuery - Run function when clicking outside element

Use Javascript/Jquery to add a SPAN inside P tag

Image Tag In HTML not clicking with jQuery

Child span tag with Jquery and HTML

Javascript function does not show the div after clicking on a href without JQuery

Random Number JavaScript In Span Tag

How to append a span tag inside another span tag in jquery?

How to take span data by clicking on the anchor tag those are wrapped in div?

Apply a javascript function in span

JavaScript - run function on <input> change - NO JQUERY

JavaScript counter up on HTML span tag element using jquery.counterup.min.js doesn't work

Javascript & jquery | I want to get the text in the <span> tag outside the button the user clicked on

JQuery Function .css with clicking action

jQuery : match keyword(will be an array) and set span tag to it

How to get the text with in span tag using jquery?

jQuery : How to access data within the <span> tag?

jQuery check if letter is in a string and wrap in span tag

Run a Javascript function when clicking outside div1 AND div2

Code on clicking javascript function is not working?

Clicking button element in function, JavaScript

Insert <span> tag using javascript replace

How to disable SPAN tag through javascript

How to prevent the onClick function to be fired when clicking on a nested span

jQuery - Different actions for when clicking on TR and TR > span

jQuery – "Read More" span not reappearing after clicking "Read Less"