Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    Is it possible to . . .

    Well, first of all, I know hardly any JS but I do have one question. Say I have a variable that is something that is two words, example "Image One: is my variable. Is there a way to turn that variable into "ImageOne?" To just remove the space? Thanks for the help.
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    Well, first of all, I know hardly any JS but I do have one question. Say I have a variable that is something that is two words, example "Image One: is my variable. Is there a way to turn that variable into "ImageOne?" To just remove the space? Thanks for the help.
    Just to make me clear something: a variable name can't have two words, so I assume that you are talking about a variable value.
    Code:
    var s = "Image One";
    s.replace(/\s+/g,'');
    regards

  • #3
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Yes, I do mean a variable value (silly me) and thank you!!!
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #4
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    Hmm, it doesn't seem to be working. Here's more of my source code:
    Code:
       if(document.getElementById(imagename) !== null){
        var image = "#" + imagename;
        location.href = image;
    Where do you think I should put it?
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    Hmm, it doesn't seem to be working. Here's more of my source code:
    Code:
       if(document.getElementById(imagename) !== null){
        var image = "#" + imagename;
        location.href = image;
    1.) here imagename is a variable and his value must be the id of a html element, something like:
    Code:
    <img id="vimag" ... />
    so imagename = 'vimag'
    2.) image must be anchor because you try to use location to go there and his value is #vimage, so you must have in your html code something like:
    Code:
    <a name="vimag"....
    3.) this do nothing if you don't use a event like onclick or something
    Maybe I could help if you say what you want to achive.

    Where do you think I should put it?
    in the head section of the (x)html page or in the body at the bottom of the section:
    Code:
    <script type="text/javascript">
    // your code here
    </script>
    if you want to put into an external file, say 'jscode.js' you can put in the head section:
    Code:
    <script type="text/javascript" src="jscode.js"></script>
    regards

  • #6
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts
    OK, well, what I have is a search function where you enter the name of the image (which is the ID div element that contains the image) and it takes you to there. The only problem is that for it to validate, the ID cannot contain spaces. What I want to do is make the IDs of the div elements the imagename without spaces and I want a script (that I already have in place) to first remove the spaces, and then go to that ID. (And I already have the else statement) so if you could give me just the "if" part, that would be great. You get it?
    ~Julian
    14 y/o web developer, drummer, and Bridge player

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Jazzo View Post
    OK, well, what I have is a search function where you enter the name of the image (which is the ID div element that contains the image) and it takes you to there. The only problem is that for it to validate, the ID cannot contain spaces. What I want to do is make the IDs of the div elements the imagename without spaces and I want a script (that I already have in place) to first remove the spaces, and then go to that ID. (And I already have the else statement) so if you could give me just the "if" part, that would be great. You get it?
    this don't work:
    Code:
    <div id="ImageOne"....
    this work:
    Code:
    <a name="ImageOne">...
    with this:
    Code:
    imagename.replace(/\s+/g,'');
    if(document.getElementById(imagename) != null){
       location.hash = imagename;
    }
    It's not tested and I assume that imagename contain the value "ImageName" and the click on something fill imagename with what is in the search box.
    You can check imagename value using a alert(imagename) before imagename.replace

    regard


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •