简体   繁体   中英

How to append '#' to the value in Javascript

Can anyone please help me how to append '#' to the value as below:

My code is below:

var idselvalue = '#'+idvalue;
console.log("IDSELVALUE"+idselvalue); // Printing as ::: #"T2"

( I want this to be printed as "#T2" so that I want to include the below)

 $('#idselvalue').val(usrObj); // to display the selected option in the select box

if I hard the value as below:

 $('#T2').val("26");

I also need help how to retrieve all the selected options, as of now I'm able to get only one first selected option

Below is my code:::

$(document).ready(function () {
            var usrObj = getCookie("selectedEXP");
            var idvalue = getCookie("selectedIDValue");


            var idselvalue = '#'+idvalue;
            console.log("IDSELVALUE"+idselvalue);
          $('#idselvalue').val(usrObj);

            console.log("OnLoad Calling usrObj"+usrObj);
            console.log("OnLoad Calling idvalue"+idvalue);

//Printing only one selected options
});

Appreciate your help! Thanks in advance :)

Try using the following:

var idselvalue = function(d){return "#"+idvalue;}

along with scrappedcola 's suggestion:

$('#idselvalue').val(usrObj); should be $(idselvalue) you already appended a # to the beginning and js can't determine the var from the string, it will just treat the entire thing as a string.

I've solved it by removing the double quotes for the idvalue and userobj as below:

 var idselvalue = '#'+idvalue.replace(/\"/g, "");
            var selusrObj = usrObj.replace(/\"/g, "");
            $(idselvalue).val(selusrObj);

It is working fine...

Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM