1

I want to create change event function to display two values that is passed from select option.

Below is my select option code. I want to pass attribute value of data-a and data-b to JavaScript so that I can use it to pass each value to each span element.

<select id="ddlEmployee" name="xAcademicYear" class="form-control" id="exampleFormControlSelect1">
    <option data-a="2015-01-01" data-b="2016-12-31" value="2015-2016">2015-2016</option>
    <option data-a="2016-01-01" data-b="2017-12-31" value="2016-2017">2016-2017</option>
    <option data-a="2017-01-01" data-b="2018-12-31" value="2017-2018">2017-2018</option>
</select>

This is where both of value will end...

<span id="curdatestart"></span>
<span id="curdateend"></span>

This is the script where I catch the data-a and data-b and throw it again by id:

$(document).ready(function(){
    $("#ddlEmployee").change(function(){
        var xDate = document.getElementById('ddlEmployee');
        var startDate = xDate.getAttribute('data-a');
        var endDate = xDate.getAttribute('data-b');
        document.getElementById("curdatestart").innerHTML = startDate;
        document.getElementById("curdateend").innerHTML = endDate;
    });
});

Please help me...

2 Answers 2

2

Try the following to get the selected options attribute:

Please Note: If you know that there is no HTML to insert into the element like in your case, it is always better to use textContent instead of innerHTML.

$(document).ready(function(){
  $("#ddlEmployee").change(function(){
    var slectedOption = this.options[this.selectedIndex];
    var startDate = slectedOption.getAttribute('data-a');
    var endDate = slectedOption.getAttribute('data-b');
    //document.getElementById("curdatestart").innerHTML = startDate;
    //document.getElementById("curdateend").innerHTML = endDate;
    document.getElementById("curdatestart").textContent = startDate;
    document.getElementById("curdateend").textContent = endDate;
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="ddlEmployee" name="xAcademicYear" class="form-control" id="exampleFormControlSelect1">
    <option data-a="2015-01-01" data-b="2016-12-31" value="2015-2016">2015-2016</option>
    <option data-a="2016-01-01" data-b="2017-12-31" value="2016-2017">2016-2017</option>
    <option data-a="2017-01-01" data-b="2018-12-31" value="2017-2018">2017-2018</option>';
</select>

<span id="curdatestart"></span>
<span id="curdateend"></span>

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

1 Comment

@AndriSetiawan, you are most welcome. Please notice the update... thanks..
2

You have some issues with your code, let's analyze them :-)

You're getting the select element rather than the selected option.

 document.getElementById('ddlEmployee');
                          ^

You're using vanilla Javascript rather than jQuery (not an issue), however, with jQuery you can use the built-in functions, i.e: data attribute and innerHTML.

var startDate = xDate.getAttribute('data-a');
                      ^
var endDate = xDate.getAttribute('data-b');
                      ^
document.getElementById("curdatestart").innerHTML = startDate;
                                        ^
document.getElementById("curdateend").innerHTML = endDate;
                                        ^

Look at this code snippet with those fixes

$(document).ready(function() {
  $("#ddlEmployee").change(function() {
    var $selected = $(this).children(':selected');
    var startDate = $selected.data('a');
    var endDate = $selected.data('b');
    
    $("#curdatestart").html(startDate);
    $("#curdateend").html(endDate);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="ddlEmployee" name="xAcademicYear" class="form-control" id="exampleFormControlSelect1">
    <option data-a="2015-01-01" data-b="2016-12-31" value="2015-2016">2015-2016</option>
    <option data-a="2016-01-01" data-b="2017-12-31" value="2016-2017">2016-2017</option>
    <option data-a="2017-01-01" data-b="2018-12-31" value="2017-2018">2017-2018</option>';
</select>

<p><span id="curdatestart"></span></p>
<span id="curdateend"></span>

See? now your logic is working and cleaner :-)

Resources

1 Comment

It is a better solution! great explanation as well. thanks lot

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.