Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript getElementBy

I have a problem to detect the option selection where instead of using getElementById i want to use getElementByValue.Does it exist?? this is my code

     <script type="text/javascript">
     var total;


function oneway(){

document.getElementById("return_date").style.visibility = "hidden";

document.getElementById("return_time").style.visibility = "hidden";

}

function return_way(){

document.getElementById("return_date").style.visibility = "visible";

document.getElementById("return_time").style.visibility = "visible";

}

function state_price(){
var way=document.getElementById("direction").value;

var placefrom = document.getElementById("state_from").value;
var placeto   = document.getElementById("state_to").value;

if (direction == "oneway"){
    if ((placefrom=="Batu Pahat" && placeto=="Alor Setar") ||(placefrom =="Alor Setar" && placeto=="Batu Pahat")){

        total=55.00;
        document.booking.total_price.value = total;


    }else if ((placefrom=="Batu Pahat" && placeto=="Bachok")||(placefrom=="Bachok" && placeto=="Batu Pahat"))
    {

        total=60.00;
        document.booking.total_price.value = total; 


    }else if ((placefrom=="Batu Pahat" && placeto=="A Famosa") || (placefrom=="A Famosa" && placeto=="Batu Pahat"))
    {

        total=65.50;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bahau")||(placefrom=="Bahau" && placeto=="Batu Pahat"))
    {
        total=70.00;
        document.booking.total_price.value = total;

    }else if ((placefrom=="Batu Pahat" && placeto=="Bentong") ||(placefrom=="Bentong" && placeto=="Batu Pahat"))
    {
        total=75.50;
        document.booking.total_price.value = total;
    }else if ((placefrom=="Batu Pahat" && placeto=="Kubang Kerian") ||(placefrom=="Kubang Kerian" && placeto=="Batu Pahat"))
    {
        total=80.00;
        document.booking.total_price.value = total;

    }

    else
    {

        document.booking.txttotal.value = "Fail";
        window.alert("Please enter a different destination");

    }
}
else
{



///something else

}
}
</script>

and this is the part of my html coding

 <tr > 
 <td>Travel Direction:</td>
 <td >

 <input type="radio" name ="direction" value = "oneway"  onclick = "oneway()"/>One Way
 <input type="radio" name ="direction" value = "return"  onclick = "return_way()"/>Return

 </td>
 </tr>

i don't want to use the name as i'm using it in my php coding.so i don;t want to use it.Is there any way where i can do my javascript as i mentioned above (getElementByValue)?Thank You in advance

like image 202
Satia Anbalagan Avatar asked Aug 23 '26 18:08

Satia Anbalagan


1 Answers

i want to use getElementByValue. Does it exist??

No. You could write your own (looping over all the elements in the document and testing them) or, if you care about initial value, rather than current value, you could use querySelector / querySelectorAll with an attribute selector.

Since you are using radio buttons, which don't change values (only checked status) under normal circumstances, that should work fine.

document.querySelector('input[value=oneway]');
like image 56
Quentin Avatar answered Aug 26 '26 07:08

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!