Mohammad Saberi
Mohammad Saberi

Reputation: 13166

jQuery change button attributes

I have a button like this:

<input type="button" name="btn" id="btn" value="Proceed"/>

After all processes and get the expected result, I need to apply some changes on this button.

  1. Change the value of this button.
  2. Add something to direct the current page to another one.

I know I can do the first goal using the code below:

$('#btn').attr('value','newValue');

But for the second one, I need something like our previous codes in JavaScript as below:

onclick="window.location.href='newPage.htm'";

Help me please.

Upvotes: 3

Views: 15552

Answers (4)

codemaniac
codemaniac

Reputation: 891

Try this:

<script type="text/javascript">
$(document).ready(function() {
    $("#btn").click(function() {
        $('#btn').attr('value', 'newValue');
        window.location="newPage.htm";
    });
});
</script>

Upvotes: 0

Irvin Dominin
Irvin Dominin

Reputation: 30993

You can do something like this :

$("#btn").val("newValue").click(function(){
document.location = "newPage.htm"; 
});

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337560

Try this:

$("#btn").attr("onclick", "window.location.href='newPage.htm'");

Or better yet, add a click handler:

$("#btn").click(function() {
    window.location.assign = 'newPage.htm';
});

Upvotes: 1

Niels
Niels

Reputation: 49919

something like, if I understand you correctly:

$("#btn").val("newValue").click(function(){
     document.location = "newPage.htm";
});

This will set a new value and bind a click handler.

Upvotes: 1

Related Questions