3

I am very new to JavaScript, and can't seem to solve a problem.

I've created a form that looks like this:

<form>
<p>
<input type="button" value="CR" ID="CR" onClick= "window.location='http://localhost/test1/html_CR.html'" />
</p>
<input type="button" value="CRI" ID="CRI" onClick= "window.location='http://localhost/test1/html_CR.html'" />
</p>
<input type="button" value="ALL" ID="ALL" onClick= "window.location='http://localhost/test1/html_CR.html'" />
</p>
</form>

I would like to create a function objectType(obj) that can include those 3 onClick events. It should look something like onClick= objectType(obj), where obj can have 3 values: CR, CRI and ALL.

Can someone help me?

1
  • The onclick event is the same on all three buttons. Is it supposed to be? Commented Jul 11, 2013 at 11:48

2 Answers 2

4

Try this out

function objectType(obj) {
    window.location = 'http://localhost/test1/html_' + obj + '.html';
}

Together with your html

<form>
<p>
<input type="button" value="CR" ID="CR" onClick= "objectType(this.value);" />
</p>
<input type="button" value="CRI" ID="CRI" onClick= "objectType(this.value);" />
</p>
<input type="button" value="ALL" ID="ALL" onClick= "objectType(this.value);" />
</p>
</form>

Because I assume you want to re-direct to the URL, which corresponds to the value of each input? :-)

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

1 Comment

@AndreiaIzabelaVug Wonderful! And for good measure, as soon as the green tick is available, you should mark this as the correct answer. This is so other people with similar problem can relate to it as a complete solution.
0

with jQuery:

$(document).ready(function() {
  $("form input").click(function() {
    window.location='http://localhost/test1/html_'+$(this).val()+'.html'
  });
});

With this u add the onClickEvent outside of the Element, u dont have to use the onClick in the input Element anymore.

$(this).val() is the value of the clicked input-object

u could give your form an ID so u can make a more accurate selector($("form input")).

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.