ASP.net How pass clientID to jScript in textbox onblur event

Enflow
Enflow used Ask the Experts™
on
ASP.net textbox onblur JS function... Need to grab textbox ID and send it to function rather than hardcode the ID in the param pass for each input object

So have this and it works with hard code name of textbox id...

onblur="CheckEnteredDate(this,'CBirthDate_1');"

would i use <%  %> ??

for this function..

function CheckEnteredDate(passed, myID) {
        //alert("This is my input value = " + myID);
        if (new Date(passed.value) > new Date()) {
            alert('STOP - The Birth Date You Entered Is Greater Than Today!');
            document.getElementById(myID).value = "";
            return false;
        }
    }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Multitechnician
Top Expert 2014
Commented:
passed.id should be fine
you can also use : passed.getAttribute("id");

onblur="CheckEnteredDate(this);"

function CheckEnteredDate(passed) {
        //alert("This is my input value = " + myID);
        if (new Date(passed.value) > new Date()) {
            alert('STOP - The Birth Date You Entered Is Greater Than Today!');
            passed.value = "";
            return false;
        }
    }

Open in new window

leakim971Multitechnician
Top Expert 2014

Commented:
another way :
<!DOCTYPE html>
<html>
<head>
    <script>
        window.addEventListener("DOMContentLoaded", function() { // https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded
            function CheckEnteredDate() {
                if (new Date(this.value) > new Date()) {
                    alert('STOP - The Birth Date You Entered Is Greater Than Today!');
                    this.value = "";
                    return false;
                }
            }
            var dates = document.querySelectorAll("input[name^='CBirthDate_']");
            for(var i=0;i<dates.length;i++)
                dates[i].addEventListener("blur", CheckEnteredDate); //  https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener
        });
    </script>
</head>
<body>
<form>
    <input type="date" name="CBirthDate_1" />
    <input type="date" name="CBirthDate_2" />
    <input type="date" name="CBirthDate_3" />
    <input type="date" name="CBirthDate_4" />
</form>
</body>
</html>

Open in new window

Add  ClientIDMode="Static" to asp:Textbox control.

Author

Commented:
@JCGD...

Why Add and Why Static... JS is working... ?
asp:TextBox has the ClientIDMode attribute to modify the way the ID is named. The "Static" value indicates that ASP do not change the value of the ID. Therefore, in the CheckEnteredDate you could have used getElementById("CBirthdate_1").value.

But the solution I think so is better

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial