Convert string to readable variable...

Posted on 2011-10-09
Last Modified: 2012-05-12

I"m looking to convert a string so my jquery script works...

The line is... var myUserID = $('#id_'+myID).val();

but it's not working because my variable "myID" uses other characters besides numbers.


How can I convert the string so it can be used in this line?

Question by:brihol44
    LVL 40

    Expert Comment

    make it

     var myUserID = $('#id_"'+myID +'" ').val();
    LVL 9

    Accepted Solution


    I don't think there should be a problem with the "-" character because it is allowed.

    Here is a list of jQuery special characters:

    What you need to do is to find a function to escape the string.
    LVL 9

    Expert Comment

    by:Roman Gherman
    here is a function to escape the selector value that I have found. please test if it works for you:

    function escapeStr( str) {
     if( str)
         return str.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1')
         return str;

    Open in new window

    LVL 81

    Assisted Solution

    If you've really something special : $("[id='id_" + myID + "']").val();
    LVL 9

    Expert Comment

    by:Roman Gherman
    Hi leakim971,

    I think in your case will not handle if myID contains '
    My opinion is that the best thing that you can do is to escape the string
    LVL 81

    Expert Comment

    You right roma1123, I hope there's no << ' >> in the id...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Hello World !, Thanks : My Sincere thanks to @Michael Male who encouraged me to write an article on this in my  blog ( Introduction:       There are several technical issues a developer may face in his…
    The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API ( has made its way into the popular lexicon of the English language.  A few years ago, …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now