PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 16, 2022

[FIXED] How to set button value in javascript

 July 16, 2022     css, html, javascript, jquery, web-deployment     No comments   

Issue

I want to set different values of button under different situations. The modal display style is "none" at first. I try to use $("#submitform").value ="Add Event"; however, it doesn't work. When the modal pops up, the button value is still empty.

var btn = document.getElementById("myBtn");
    btn.onclick = function() {
        $("#submitform").value ="Add Event";
        modal.style.display = "block";
    };
<div id = "myModal" class = "modal">
<form name="addEvtForm" class="addEvtForm" >
  title:<input type="text" name="title" id="title" autocomplete="title"><br>
  <input type="button" value="" id="submitform">
</form>
</div>


Solution

First, you didn't add jQuery to the html, so the .value didn't work. Second, there was invalid syntax for your code. I fixed it up:

$("#submitform").click(function() {
        $('#submitform').val('Add Event');
        $(".modal").css("display", "block");
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id = "myModal" class = "modal">
<form name="addEvtForm" class="addEvtForm" >
  title:<input type="text" name="title" id="title" autocomplete="title"><br>
  <input type="button" value="" id="submitform">
</form>
</div>

This should work.



Answered By - Random Channel
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing