Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7707299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:19:29+00:00 2026-06-01T00:19:29+00:00

I have an input that holds a date value like so 03/15/2012 I am

  • 0

I have an input that holds a date value like so

03/15/2012

I am trying to select only portions of the the value instead of the whole thing. For instance if I click the spot before 2 in 2012 the year 2012 will be selected not the whole date (same for is true for months and day).

This is the code I am working with now

html:

<input class = "date-container" />

javascript/jquery:

$('.date-container').on('select', function (e)
    e.preventDefault()
    this.onselectstart = function () { return false; }; 
})

$('.date-container').on('focus', function ()
{
    if (document.selection) {
        this.focus();
        var Sel = document.selection.createRange();
        Sel.moveStart('character', -this.value.length);
        CaretPos = Sel.text.length;
    }

    // Firefox support

    else if (this.selectionStart || this.selectionStart == '0')
        switch (this.selectionStart) {
        case 0:
        case 1:
           this.selectionStart = 0;
           this.selectionEnd = 1;
           break;
     }

}

I have tried a couple things so far. The code above is attempting to prevent the normal select action then based on where the focus is, select a portion of the string(I only have the switch statement options for the month portion, but if it worked I would do the same for day and year). This may be the wrong way to go about it.

Things to note:

By select I mean highlight.

I do not want to use plugins.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T00:19:31+00:00Added an answer on June 1, 2026 at 12:19 am

    This code will select the portion of the date that is clicked on:

    $(".date-container").click(function() {
        var val = $(this).val();
        var sel = this.selectionStart;
        var firstSep = val.indexOf("/"), secondSep;
        if (firstSep != -1) {
            secondSep = val.indexOf("/", firstSep + 1);
            if (secondSep != -1) {
                if (sel < firstSep) {
                    this.setSelectionRange(0, firstSep);
                } else if (sel < secondSep) {
                    this.setSelectionRange(firstSep + 1, secondSep);
                } else {
                    this.setSelectionRange(secondSep + 1, val.length);
                }
            }
        }
    });​
    

    You can see it work here: http://jsfiddle.net/jfriend00/QV4VT/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build something on javascript that I can have an input that
I have a string that holds user input. This string can contain various types
We have some input data that sometimes appears with &nbsp characters on the end.
I have an input file that I want to sort based on timestamp which
I have an input box that takes values from 0-100. I want to prevent
I have an input field that is by default set to type=text so that
I have 2 input boxes that are not disabled. When either gets focus no
I have a string input that i do not know whether or not is
I hope I can explain this right I have two input fields that require
In my ASP.NET page, I have an input box that has to have the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.