Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:34:25+00:00 2026-05-25T17:34:25+00:00

The script implications are more advanced, but I’ve condensed it down to this following

  • 0

The script implications are more advanced, but I’ve condensed it down to this following problem:

In a nutshell I have some keyword variables set with javascript. These keyword variables contain URLs. What I need is when a person inputs a keyword he will be taken to a specific URL. I’ve created a sample page illustrating my problem:

<a href=""  class="link">LINK</a><br>
<input type="text"  class="text" value="hallo" />
<input type="button" onclick="check();" value="Click ME">

<script type="text/javascript">
function check() {
    var key1 = "some_link1.com";
    var key2 = "some_link2.com";
    var key3 = "some_link3.com";

    var l = document.getElementsByClassName("link")[0];
    var t = document.getElementsByClassName("text")[0];
    if (t.value == "key1" || t.value == "key2" || t.value == "key3") {
        l.href = t.value;
        alert(t.value);
    } }</script>

The problem lies between the ** because the script must recognize that the value corresponds to the variable and use thet value instead. However currently I’m only getting the inputted value. By the way I can’t use a form or reload the page. It has to happen “live”.

Any thoughts?

PS. the script is not meant to run, but to illustrate my point:
http://jsfiddle.net/UhgKG/3/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:34:26+00:00Added an answer on May 25, 2026 at 5:34 pm

    Partly because I avoid eval() when there are other ways, but also because it just seems cleaner, is more extensible and is less code, I’d do it like this with an object lookup rather than the separate variables, the multiple if tests and the eval:

    <a href="" id="link">LINK</a><br>
    <input type="text" id="text" value="hallo" />
    <input type="button" onclick="check();" value="Click ME">
    
    <script type="text/javascript">
    function check() {
        var keys = {
            key1: "some_link1.com",
            key2: "some_link2.com",
            key3: "some_link3.com"
        };
    
        var l = document.getElementById("link");
        var t = document.getElementById("text").value;
        if (t in keys) {
            l.href = keys[t];
            alert(t);
        }
    }
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The script have some hard-coded relative paths. I would like them to be relative
The script below will replace selected word in a textarea. But it only works
What are the performance hit or implications, if any, of: If I have multiple
Script adds id for block, gives: <div id=some>Text</div> After that page should jump for
Problem: I have a user who would like to generate an HTML page directly
This script I wrote works perfect on IE! It works on Chrome and FireFox
This script shows and positions a div like: function myfunction() { obj.style.visibility = visible;
JRuby script.rb throws many Java errors. I tried outputting to a text file, but
Action script is developed based on object oriented programming but why does it not
Write Script to read a positive integer number then it computes the following sequence:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.