0

im trying to generate a element dynamically and bind the element to a click event using the below code..Everything works fine but the click event is not being triggered when I click datasource.

function create_datasource_facet(facetcount){
        console.log(JSON.stringify(facetcount))
        $('#filters').append('<div id="datafacet"></div>')
        $.each(facetcount,function(index,value){
                console.log(value);
                console.log(index);
                $('#datafacet').append('<br>')
                $('#datafacet').append('<div id="datasource">' + index + '[' + value + ']  </div>')
                //$('#datafacet').append('<br>')

        })

$(document).on('click','#datssource',function(){ alert('datasource clicked')})

}
2
  • Spelling mistake in $(document).on('click','#datssource',function() Commented Sep 23, 2014 at 15:09
  • also note that even though javascript has ASI, strict browsers can make problems with that. so better close everything with semicolons just to be on the safe side. Commented Sep 23, 2014 at 15:11

4 Answers 4

1

You have a typo in the ID value here:

$(document).on('click','#datssource',function(){ alert('datasource clicked')})
----------------------------^
Sign up to request clarification or add additional context in comments.

Comments

1

You've misspelt #datasource in your click event handler.


One thing to be aware of is that you're potentially trying to append multiple elements with the id '#datasource'. IDs should be unique, so you're probably better off making this into a class .datasource.

2 Comments

@isherwood this is true. Removed that, and added in a note about duplicate IDs which he is likely to run into
I didn't catch the duplicate IDs. Well spotted. OP could also increment the IDs.
0

click Event on #datasource not #datssource

function create_datasource_facet(facetcount){
        console.log(JSON.stringify(facetcount))
        $('#filters').append('<div id="datafacet"></div>')
        $.each(facetcount,function(index,value){
                console.log(value);
                console.log(index);
                $('#datafacet').append('<br>')
                $('#datafacet').append('<div id="datasource">' + index + '[' + value + ']  </div>')
                //$('#datafacet').append('<br>')

        })

$(document).on('click','#datasource',function(){ alert('datasource clicked')})

}

1 Comment

You've misspelled the ID value, too. :-)
0

Think that your code is load before the document is loaded.

try it

$('#datssource').on('click',function(){ alert('datasource clicked')})

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.