Jump to content

Recommended Posts

I have a couple of functions as shown below

 

 
function displayTrackingNumber()
{
if (document.getElementById('cboOrderStatus').value == 'Shipped') {
        document.getElementById('txtTrackingNumber').style.display = '';
document.getElementById('txtTrackingNumber').focus();
    } else {
        document.getElementById('txtTrackingNumber').style.display = 'none';
    }
}
 
function blankTrackingNumber()
{
if(this.value =='Tracking Number') {
this.value ='';
}
}
function checkTrackingNumber()
{
if(this.value =='Tracking Number') {
alert("Please enter a Tracking Number!");
}
if(this.value =='') {
alert("Please enter a Tracking Number!");
}
}

 

They are implemented on the page as shown here

 

<input name="txtTrackingNumber" id="txtTrackingNumber" value="Tracking Number" style="display:none" onkeydown='blankTrackingNumber()' onblur='checkTrackingNumber()' />

 

But when I leave the field nothing is happening and there is nothing appearing in the console and when I press a key on the keyboard the value 'Tracking Number is staying in the field.  

Link to comment
https://forums.phpfreaks.com/topic/285812-javascript-not-working/
Share on other sites

Thought of using the HTML5 placeholder attribute instead.

 

But to answer you question you need to call the blankTrackingNumber on the focus event not keydown event

<script>
function displayTrackingNumber()
{
    console.log(document.getElementById('cboOrderStatus').value);
    if (document.getElementById('cboOrderStatus').value == 'Shipped') {
            document.getElementById('txtTrackingNumber').style.display = '';
            document.getElementById('txtTrackingNumber').focus();
    } else {
        document.getElementById('txtTrackingNumber').style.display = 'none';
    }
}
function blankTrackingNumber(elm)
{
    if(elm.value == elm.defaultValue) {
        elm.value ='';
    }
}
function checkTrackingNumber(elm)
{
    if(elm.value == undefined || elm.value =='' || elm.value == elm.defaultValue) {
        alert("Please enter a Tracking Number!");
        elm.value = elm.defaultValue;
    }
}
</script>
<body onload="displayTrackingNumber()">
<form>
    <input type="text" id="cboOrderStatus" name="cboOrderStatus" value="Shipped" />
    <input name="txtTrackingNumber" id="txtTrackingNumber" value="Tracking Number" style="display:none" onfocus="blankTrackingNumber(this)" onblur='checkTrackingNumber(this)' />
</form>
</form>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.