nick gowdy
nick gowdy

Reputation: 6511

HTML onclick document.getElementById

I have a question which I am sure is simple but I have slight problem getting it to work. I have wrote a function in an external js file which has one parameter.

In my HTML I have onclick event where the arguement I want to supply is the value in a dropdown menu, hence me using document.getElementById to grab that value. But when I trigger the event it is complaining.

<select id="ddlOriginHubChinaSea" onclick="chkHub(document.getElementById('ddlOriginHubChinaSea').val())" >
                <option value="Cambodia">Cambodia</option>
                <option value="HK">HK</option>
                <option value="Indonesia">Indonesia></option>
                <option value="Shanghai">Shanghai</option>
                <option value="Thailand">Thailand</option>
</select>


function chkHub(param) {  
    if (param != '') {
        $('#txtChinaSeaAirHub').val(param);       
    }
    else {
        $('#txtChinaSeaAirHub').val('');
    }
}

Upvotes: 1

Views: 2509

Answers (2)

Rab
Rab

Reputation: 35572

function chkHub(param) {  
    if (param.options[param.selectedIndex].value != '') {
        $('#txtChinaSeaAirHub').val(param.options[param.selectedIndex].value);       
    }
    else {
        $('#txtChinaSeaAirHub').val('');
    }
}

and onchange="chkHub(this)"

Upvotes: 1

epoch
epoch

Reputation: 16595

Try this out:

<select id="ddlOriginHubChinaSea" onchange="chkHub(this.value)" >

Upvotes: 6

Related Questions