0

I have a button and I am changing its value and name on click showing same value in alert. Its working fine when I use jQuery but not when I use call function with JavaScript function value is not changing in front view but in alert value is changing its totally strange.

Here are the demos

JavaScript

function change() {
  if ($(this).attr('name') == 'first') {
    $(this).attr('name', 'second');
    $(this).attr('value', 'second');
    alert('new name ' + $(this).attr('name'));
  } else {
    $(this).attr('name', 'first');
    $(this).attr('value', 'first');
    alert('new name ' + $(this).attr('name'));
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="submit" id="button" value="first" name="first" onclick="change()">

jQuery

$('#button').click(function() {
  if ($(this).attr('name') == 'first') {
    $(this).attr('name', 'second');
    $(this).attr('value', 'second');
    alert('new name ' + $(this).attr('name'));
  } else {
    $(this).attr('name', 'first');
    $(this).attr('value', 'first');
    alert('new name ' + $(this).attr('name'));
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="submit" id="button" value="first" name="first">

I know I can use jQuery as I am already including the library but I so much wanted to know what is the difference between these two.

7
  • Er... Never change name of input. General thumb rule. Commented Jul 12, 2016 at 12:41
  • Just a suggestion: Use val("value") instead of attr("value","value") Commented Jul 12, 2016 at 12:42
  • 2
    Both of them are jQuery Commented Jul 12, 2016 at 12:43
  • 1
    @GauravAggarwal You are just setting the attribute. It doesn't change the value. Commented Jul 12, 2016 at 12:44
  • 1
    @Sree i know and i already mentioned in question i am including jquery for both but its all about calling function by jQuery or javascript Commented Jul 12, 2016 at 12:45

2 Answers 2

4

You need to pass this to change function so that you can access the clicked object inside that function, .click() event of any element will automatically detect $(this) but in function you need to pass it

Pass this in change() function in button:

<input type="submit" id="button" value="first" name="first"  onclick="change(this)">

So change function will have:

function change(obj) {
  if ($(obj).attr('name') == 'first') {
    $(obj).attr('name', 'second');
    $(obj).attr('value', 'second');
    alert('new name ' + $(obj).attr('name'));
  } else {
    $(obj).attr('name', 'first');
    $(obj).attr('value', 'first');
    alert('new name ' + $(obj).attr('name'));
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="submit" id="button" value="first" name="first"  onclick="change(this)">

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

2 Comments

Explanation being, change() is bound (sets "this") to the window, not the button, so you need to pass it to the function to access it. In the second example, jquery binds the function to the button, so $(this) is as you expect
@GauravAggarwal You need to pass this to change function so that you can access the clicked object inside that function...click event of any element will automatically detect $(this).. but in function you need to pass it
0

passing 'this' to the function and using it instead of 'this' inside the function solves your problem.

because using 'this' inside a function not handled by Jquery event, returns the owner of the function that is an object of the button you have while in jquery it returns a jQuery object

function change(btn){
	if($(btn).attr('name') == 'first'){
  	$(btn).attr('name','second');
    $(btn).attr('value','second');
    alert('new name '+$(btn).attr('name'));
  } else {
  	$(btn).attr('name','first');
  	$(btn).attr('value','first');
    alert('new name '+$(btn).attr('name'));
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input type="submit" id="button" value="first" name="first"  onclick="change(this)">

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.