Reputation: 35
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?
Upvotes: 3
Views: 79
Reputation: 6158
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")).
Upvotes: 0
Reputation: 18922
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? :-)
Upvotes: 4