samsri
samsri

Reputation: 1104

using target attribute with form to open the link in same page

I am developing a web app. I used form as a drop down menu so that it displays the information about the category users selected. But when I select an option the link is opening in new window. I want it to open in the same window. I used target attribute with form like this

<form id="aform" target="_self">
<select id="mymenu">
<option selected>Choose a category</option>
<option value="moblist.html" target="self">Mobiles</option>
<option value="" >Tablets</option>
<option value="">Laptops</option>
<option value="">Bikes</option>
<option value="">Car</option>
</select>
</form>

my java scrip is

var selectmenu=document.getElementById("mymenu")
selectmenu.onchange=function(){ //run some code when "onchange" event fires
var chosenoption=this.options[this.selectedIndex] //this refers to "selectmenu"
if (chosenoption.value!="nothing"){
window.open(chosenoption.value, "", "") //open target site (based on option's value                attr) in new window
}
}

but still the link is opening in new window. How can I make the selected page to open in the same window.

Upvotes: 0

Views: 3029

Answers (3)

hjpotter92
hjpotter92

Reputation: 80639

With the

window.open(chosenoption.value, "", "")

you get a new window. You'll need to set the page's src value to chosenoption.value.

Upvotes: 1

Yatrix
Yatrix

Reputation: 13775

Window.open opens a new browser window. I found this on this site:

How to redirect to another webpage in JavaScript/jQuery?

Upvotes: 0

techfoobar
techfoobar

Reputation: 66663

You can remove the target attribute altogether.

<form id="aform">
...

should be enough to open it in the same window.

Upvotes: 0

Related Questions