Changing border-color of HTML input element to red if an error occurs

On November the 7th a question was posted on SO where the OP wanted to change the border color of the input elements to red when both inputs didn’t match a condition.

I combined the JavaScript and a little bit of JQuery to solve this.

Here is the code:

var index_one;
var index_two;

    $('#Location, #LocationTo').change(function (e) {

        index_one = $("#Location")[0].selectedIndex;
        index_two = $("#LocationTo")[0].selectedIndex;
    });
    $("#submit").click(function () {
        if ($("#Location>option").eq(index_one).text() == $("#LocationTo>option").eq(index_two).text()) {
            $('#Location, #LocationTo').css("border-color", "red");
        } else {
            $('#Location, #LocationTo').css("border-color", "black");
        }
    });

As you can see, I first fetched the indexes of the selected items and after that I gave the index to the eq(index) jquery function in order to get the element as a object and then get the text via the .text() function. If the same options were selected in both input tags then the red border color will be displayed otherwise the black default color stays.

If you want to handle all the possible cases then add the following code:

else if($("#Location>option").eq(index_one).text() =="Please Select")
    {
        alert ("Select an option");
    }
      else if($("#LocationTo>option").eq(index_one).text() =="Please Select")
    {
        alert ("Select an option");
    }

Here is the fiddle to see the code in action: http://jsfiddle.net/eugensunic/eg8k4vL7/1/

Adding text from div to another div atribute inside a (stackoverflow problem)

Problem posted: 07/08/2015

url: http://stackoverflow.com/questions/31871404/how-to-add-text-in-the-data-attribute-using-jquery
fiddle: http://jsfiddle.net/eugensunic/0kourse6/4/

Problem description:

The title says it all. The user wanted to fetch the text from the div tag and assign it to the

atribute which had a parent li.

Solution to the problem:

var array=[];
$('.pin_head > div').each(function (index,obj) { 

   array[index]= $(this).text();

});

$('li > div').each(function (index,obj) { 
    var data= $(this).attr("data-col",array[index]);
});