2

I have a div that shows different html each time a user is selecting different options from a drop down menu. What I am trying to achieve is manipulate the id's that the div outputs.

This is my code for the html drop down:

<select name="surveys" id="surveys" required>
           <option value="">Select a Survey</option>
            <cfloop query="application.clientAdminSurveys">
               <option value="#name#">#name#</option>
            </cfloop>
        </select>

This is the JS that controls the drop down:

 $('#surveys').change(function (event) {

     var survey = $('#surveys').val().toLowerCase();

     if(survey.indexOf('consultation') !== -1 && survey.indexOf('cancellation') === -1 ){
         $('.type').show();
         $('.type').html('<input type="text" id="consultServiceType" name="consultServiceType" placeholder="Consult Service Type"  /> <input type="text" id="eventDate" name="consultDate" placeholder="Consult Date"  /><input type="text" id="consultantName" name="consultantName" placeholder="Consultant Name" /> '); 
     }else if(survey.indexOf('cancellation') !== -1){
         $('.type').show();
         $('.type').html('<input type="text" id="eventDate" name="cancellationDate" placeholder="Cancellation Date" />');
     }else if(survey.indexOf('procedure') !== -1 && survey.indexOf('cancellation') === -1){
         $('.type').show();
         $('.type').html('<input type="text" id="serviceType" name="serviceType" placeholder="Service Type"/><input type="text" id="eventDate" name="serviceDate" placeholder="Service Date" /><input type="text" id="serviceProviderName" name="serviceProviderName" placeholder="Service Provider Name" /><input type="text" id="serviceRevenue" name="serviceRevenue" placeholder="Service Revenue" />');
     }else if(survey.indexOf('inquiry') !== -1){
         $('.type').show();
         $('.type').html('<input type="text" id="eventDate" name="inquiryDate" placeholder="Inquiry Date" />');
     }else{
         $('.type').hide();
     }
 });

How I output the JS code:

 <div class="type"></div>

The HTML output from firebug:

<div class="type">
  <input id="consultServiceType" type="text" placeholder="Consult Service Type" name="consultServiceType">
  <input id="eventDate" type="text" placeholder="Consult Date" name="consultDate">
  <input id="consultantName" type="text" placeholder="Consultant Name" name="consultantName">
</div>

I need to be able to manipulate the ID that comes from the output of the div. Any ideas how I can do that? When I do this:

$('.type').mouseenter(function() {
    alert('hello!');
});

I get the alert as I should but I want to drill down to <input id="eventDate" type="text" placeholder="Consult Date" name="consultDate"> with something like:

 $('.type:input#eventDate').mouseenter(function() {
        alert('hello!');
 });
2
  • Since it's an id selector (ids should be unique), you can just use $("#eventDate"). Anyways, you needed a space in your selector - $('.type :input#eventDate'), although you need event delegation if your elements are dynamic Commented May 24, 2013 at 16:39
  • the answer by pXL below solved the issue. thanks for the comment! Commented May 24, 2013 at 16:43

1 Answer 1

5

Use event delegation - (as you are adding #eventDate dynamically)

 $('.type').on('mouseenter','#eventDate',function() {
        alert('hello!');
 });

what is event delegation ? ---> http://learn.jquery.com/events/event-delegation/

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I will accept it in 10 minutes because now it doesn't let me. Thanks for the link as well. First time that I have to do this so it will help for later projects!

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.