JQuery - Get value after delimiter alternative?

JQuery - Get value after delimiter alternative?



I've already searched this up before and tried using both pop() and lastIndexOf but it doesn't work well on looped dropdown.


pop()


lastIndexOf



Is there any alternative to getting the value after a delimiter, one that does not remove it from the list?



Here's my code:


$(document).ready(function ()
$('#VehicleID').change(function ()
var vehdrv = ($('#VehicleID :selected').val());
var drvid = vehdrv.split("_").pop();
var vehid = vehdrv.split("_")[0];

$('#VehicleID :selected').val(vehid);
//alert($('#VehicleID :selected').val());

if (drvid > 0)
$('#DriverID').find("option").show().not("option[value*='" + drvid + "']").hide();

$("#DriverID").val(drvid);

//$("#DriverID").val(
//$("#DriverID").find("option:visible:first").val())


else
$("#DriverID").find("option").show();

$("#DriverID").val(
$("#DriverID").find("option:visible:first").val())

).trigger("change");
)



My Dropdowns from the controller:


public ActionResult ReserveInitial()

ViewBag.VehicleID = db.Vehicles.Where(item => item.IsReserved == false).Select(a => new SelectListItem

Value = a.VehicleID.ToString() + "_" + a.DriverID.ToString(),
Text = a.VehicleMake + " - " + a.PlateNumber,
);

ViewBag.DriverID = db.Drivers.Select(a => new SelectListItem

Value = a.DriverID.ToString(),
Text = a.DriverLastName + ", " + a.DriverFirstName
);


return View();



EDIT
This is how the string would look like if the VehicleID is linked to a Driver:
"14_5"



if the VehicleID is not associated with a Driver it would look like this:
"15_0"





When posting a question about finding element(s) in a string, please add in a couple of examples of what the original elements look like, for instance "xxx_yyy_zzz" or something. It is hard to deduce from your code what you expect the strings to look like.
– Chris Cousins
Aug 23 at 16:53





@ChrisCousins Thanks for the advice, added how the string would look like.
– Joshua
Aug 23 at 17:05




2 Answers
2


var parts = vehdrv.split("_");
var drvid = parts[1];
var vehid = parts[0];





Still the same result, it removes the drvid value after the same vehid value is picked again.
– Joshua
Aug 23 at 17:19



You can try this




var vehdrv = $('#VehicleID').val().match(/[^_]+/g);
var drvid = vehdrv[0];
var vehid = vehdrv[1];

console.log(drvid,vehid);


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="VehicleID" type="text" value="14_5" name="text">






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)