1

I have a bit of javascript that finds an existing id and replaces it with a form.

Within that form I'd like to be able to add javascript variables to the submit url. I'm not sure if that's possible or if I'm just implementing it incorrectly.

$(document).ready(function () {
    $("#FB_cs_redirectTextBox")
    .replaceWith('<form class="cf" action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value" method="get">' +
                '<input type="search" placeholder="Search for stuff" name="search" class="searchbox-input" required="">' +
             '<select name="item-select" id="item-select" required="">' +
                '<option value="" selected="selected">Item type...</option>' +
                '<option value="level">item 1</option>' +
                '<option value="taste">item 2</option>' +
                '<option value="location">item 3</option>' +
                '<option value="month">item 4</option>' +
                '</select>' +   
             '<button type="submit" class="btn btn--green searchbox-submit">' +
                    'Search for items' +
                '</button>' +
            '</form>');


 });

So this is my problem line I think:

action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value"

I was also wondering if this is possible, how I could also add the value of the select to the url action. Would I do this by applying the same id to all the options and then calling document.getElementById on that id name?

All help greatly appreciated!

3
  • Possible duplicate : stackoverflow.com/questions/993834/… Commented Jun 18, 2015 at 14:32
  • What is one of the expected URLs for action? Something like http://test.com/s/search.html?profile=_default&collection=generallevel? Commented Jun 18, 2015 at 14:32
  • Whoops missed a bit off it should be: test.com/s/… then the variable amended here Commented Jun 18, 2015 at 14:35

3 Answers 3

2

Give your form a name, then:

document.form_name.action = 'http://test.com/s/search.html?profile=_default&collection=general' + document.getElementById(' + item-select + ').value;

Make sure you do this after the DOM is loaded, otherwise it won't work.

Sign up to request clarification or add additional context in comments.

1 Comment

The name wasn’t the main problem. You also changed the string concatenation around document.getElementById, right? This was the actual issue.
1

Replace your
This

action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value"

With

action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(item-select).value"

You need to put ' and + only if 'item-select' is a variable but its not, its an ID, so remove ' and + marks. codes work

Comments

1

Change this

'<form class="cf" action="http://test.com/s/search.html?profile=_default&collection=general + document.getElementById(' + item-select + ').value" method="get">'

to this

'<form class="cf" action="http://test.com/s/search.html?profile=_default&collection=general&search=' + document.getElementById('item-select').value + '" method="get">'

Then the JavaScript syntax is correct again.

These are the expected URLs for action:

http://test.com/s/search.html?profile=_default&collection=general&search=level
http://test.com/s/search.html?profile=_default&collection=general&search=taste
http://test.com/s/search.html?profile=_default&collection=general&search=location
http://test.com/s/search.html?profile=_default&collection=general&search=month

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.