0

Thank you in advance.

I am still learning JavaScript and as a project approached, really need some help/insight.

The logic that I am working on is this:

1. HTML structure:

<input title="Type Here" class="search" type="text" placeholder="Type Here">

<select id="device_select">
<option id=one value='a'>a</option>
<option id=two value='b'>b</option>
<option id=three value='c'>c</option>
<option id=many value='many'>many</option>
</select>

<span class="content-btn-1" type="button"></span>

2. JS structure:

$(function(){

var one = {title:"titletext", description:"descrtext", keyword:"text",
subject:"533,567,457", provider:"c9drlt-sdgtrzz", training:"true"};

var two = {title:"titletext", description:"descrtext", keyword:"textthis",
subject:"537", provider:"c9drlt-sdgtrjt", training:"false"};

});

3. JS logic structure:

function search_class() {
if (training == true) {training = "&tr=0";} else {training = "";}
return training;
}

function search_custom() {  
// NOT SURE HOW TO PULL IT UP
// if subject has more than 1 variable like subject:"533,567,457"
// then construct the logic to separate them:
// &s=533&s=2&567&s=3&457
return subject;
}

var url = "www.website.com";
var text_area = $('.search');
var btn_click = $('.content-btn-1');

btn_click.click (function () {
var value = text_area.val();
var ty = "#s=";
if ($.trim($(text_area).val())) {
window.location.href = url+ty+search_class()+search_custom();
}
});

4. The outcome:

www.website.com#s=titletext&d=descrtext&t=text&p=c9drlt-sdgtrzz&tr=0&s=533&s=2&567&s=3&457

5. The hard part: How can we do the logic so it takes that array in #2, attaches to the option id one, two ... etc in #1 and then constructs the link in #3 on click?

In nutshell: This is a search function with options that has unique variables.

Appreciate any help!

1 Answers1

0

First off, I think you want to clean up your select box a bit, and give it values you can actually use in javascript.

<input title="Type Here" id="search_text" type="text" placeholder="Type Here" />

<select id="device_select">
    <option value='1'>a</option>
    <option value='2'>b</option>
    <option value='3'>c</option>
    <option value='0'>many</option>
</select>

<input id="submit_button" type="button" />

Then we need to clean up your object a bit so it's a) more readable, and b) it links to your select box nicely. If we nest it into one object, referencing it later will be easier.

// we would like an array that corresponds with our values in the select box.
var options = [
    false,
    {
        title:        "titletext",
        description:  "descrtext",
        keyword:      "text",
        subject:      "533,567,457",
        provider:     "c9drlt-sdgtrzz",
        // a boolean like "false" or "true" should not be surrounded by braces, is easier to manipulate.
        training:     true
    },{
        title:        "titletext",
        description:  "descrtext",
        keyword:      "textthis",
        subject:      "537",
        provider:     "c9drlt-sdgtrjt",
        training:     false
    }
];

The final bit is in the logic. I've tried to streamline it a bit but the most important change is the for-loop in this case, as it will list every key/value pair and then we can add it to our search string. I Have also included some error dodging here, like checking if theres empty values and if the selected values actually exist (for example, if you select 3 in device_options, we don't have that index in our options array above, so we can't actually construct that - but it won't actually error out.)

$("#submit_button").on("click", function(event){
    // now for the hard part, converting your select item into a string and appending the text
    // 1. get the search string from the input
    var searchstring = "#searchText=" + $("#search_text").val().trim();
    // 2. get the select box value of the selected option
    var selected = $("#device_select").val();
    // 3. Get the corresponding value. 
    // If the corresponding value is false (remember the first item in the array above?), then we do nothing.
    // We also do nothing if we are looking for an undefined number in the array (say, item #238)
        selected = typeof options[selected] != "undefined" && options[selected] !== false
            ? options[selected]
            : false;
    // if selected was not set to false, then it exists in our options and we can use it.
    if(selected){
        for(key in selected){
            // if you set a value in options to false, we'll ignore it here using a 'continue'
            // continue will move on to the next iteration of the for loop immediately
            if(!selected[key]) continue;
            searchstring += "&" + key + "=" + selected[key];
        }
        window.location.href = "http://www.mysite.tld/" + searchstring;
    } else {
        // log an error here.
        if(console) console.log("Could not find device!");
    }
});

Does this make sense/help? I have not tested this myself, but it should all work.

somethinghere
  • 16,311
  • 2
  • 28
  • 42
  • Somethinghere, this is great! thank you a lot for such a fast response...indeed helped a lot. –  Oct 28 '14 at 16:23
  • Another quick question...if in the options "training:false," is it possible to remove that line (or any other line with false or "") completely before it is passed to the searchstring? –  Oct 28 '14 at 17:27
  • ow yeah I forgot about that. I'll amend it. Its amended, I added a `if(!selected[key]) continue,` – somethinghere Oct 28 '14 at 17:29
  • You are awesome. The "options" object/array is more for user friendly navigation, so later I change window.location.href = "http://www.mysite.tld/" + searchstring.replace(/title/, 't'); etc. Also, if you can guide to the right direction with subject:"533,567,457". I am not sure how to do it, but each number would build separate logic like if more then 1 number > then it creates &s=533&s=2&567&s=3&457 and only after add to link –  Oct 28 '14 at 19:14
  • For the kind of functionality you could extend it in your for loop, using something like `if(typeof selected[key] === "object"){ // nest another for loop here to create a string }`. However once you start nesting objects and running through them, it might be better to create a function like 'stringifyObject(object)' that returns a string, but when a value is an object it calls itself and returns that result. Does that make sense? More here: http://stackoverflow.com/questions/16508582/looping-through-a-triple-nested-object-in-javascript – somethinghere Oct 28 '14 at 21:23
  • Your suggestions worked wonders. I have quick related question that I couldn't find answer for... when you have a chance. This is the question: Looking back to Options, if we think of it as the checkboxes. What if the user select 2 checkboxes that looks like this var options = [ false, { title: "checkbox-1", field: "id=345,value=31;" },{ title: "checkbox-2", field: "id=345,value=32;" } ]; What would be the way to combine the fields ids and values to end up with only one set: id=345, value=32,32,33...instead id=345,value=31;&id=345,value=32 –  Nov 07 '14 at 00:37
  • You would probably have to nest another object, so `field : {id : 345, value : 31}`. Then you would access that field id with object.field.id . I think thats what you are asking but i'm not sure (: – somethinghere Nov 07 '14 at 07:05
  • Sorry, edit time was over! --> In that case you would want to make a new object, loop through your existing one and create a key with the id, and a nested arrray in there. So: `var result = {}; foreach(thing in options){ if(/*checked*/){ if(typeof result[options[thing].id] == "undefined"){ result[options[thing].id] = new Array(options[thing].value); } else { result[options[thing].id].push(options[thing].value) }}}`. Here I build a new object, if the key in the object doesnt exists I create it with a new array of options, otherwise I add it to the array. Then you can stringify from there. – somethinghere Nov 07 '14 at 07:12