This question already has answers here:
Event binding on dynamically created elements?
(23 answers)
Closed 11 months ago.
I created a div as defined below
<div class="area1">
</div>
With this ajax call I changed the html for above div
$('#abutton').click(function(e) {
e.preventDefault();
$.ajax({
url: 'do',
type: 'POST',
dataType: 'json',
cache: false,
data: {id:this.id},
success: function(data) {
$('.area1').html(data);
},
failure: function() {
alert('Please try again..!');
}
});
});
now div content looks like this
<div class="area1">
<input type="text" placeholder='Name'>
<button id='submit'>Submit</button>
</div>
Now I want to perform some action on button click
$('.area1 button').click(function(e){
e.preventDefault();
alert(this.id) ;
});
but this second ajax is not selecting the button
Can anyone tell the correct method
$(document).on("click","div.area1 button",function(e) {
e.preventDefault();
alert(this.id) ;
});
you can try this
if your page was dynamically creating elements dosomething you would bind the event to a parent which already exists (this is the nub of the problem here, you need something that exists to bind to, don't bind to the dynamic content), this can be (and the easiest option) is document.
Related
I am having some difficulties in getting text from a url and assigning the retrieved data into a form input field. When I use a div the code works but i want to use an input text field.
Below is my jQuery:
<script>
$(document).ready(function(){
$("#torefresh").load("myurl.php");
setInterval(function() {
$("#torefresh").load("myurl.php");
}, 100);
});
</script>
This is my html:
<div data-value="" id='torefresh'></div>
It works with the above div.
<input type='text' readonly="readonly" name='torefresh' id='torefresh'>
But i want the input text solution
Thanks in advance
From the jQuery .load() documentation:
jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document.
However, the input element doesn't have an innerHTML property - you must use the value property (or .val() in jQuery-ese). Therefore, you cannot use .load() to do this.
Since .load() is just a shorthand for $.ajax(), you can use that instead.
Try:
<script>
$(document).ready(function(){
get_new_data();
setInterval(function() {
get_new_data();
}, 100);
});
function get_new_data(){
$.ajax({
type: 'get',
url: 'myurl.php'
}).done((retn) => {
$('#torefresh').val(retn);
});
}
</script>
I'm using an icon font on my website and I want users to vote on their favorite icon. the icon font uses and CSS to display the icon. How can I accomplish this using some type of jQuery and returning the icon to the front end along with passing it value through a form?
I know I can't add a inside of an input, so i took this approach:
I started with this jsFiddle: http://jsfiddle.net/6NVpL/42/
HTML:
<div class="iconDisplay">Display's selected icon</div>
<button class="selectIcon">Select Icon</button>
<div id="iconSelector">
<span class="icon-icon1"></span>
<span class="icon-icon2"></span>
<span class="icon-icon3"></span>
<span class="icon-icon4"></span>
<span class="icon-icon5"></span>
<span class="icon-icon6"></span>
<span class="icon-icon7"></span>
<span class="icon-icon8"></span>
</div>
JS:
$(".selectIcon").click(function () {
$("#iconSelector").fadeToggle();
});
$("#iconSelector span").click(function () {
$(this).click(function(){
$("#iconSelector").hide();
});
});
Maybe this is more what you are looking for?
I fixed the click handlers:
$("#selectIconButton").click(function () {
$("#iconSelector").fadeToggle();
});
$("#iconSelector span").click(function () {
selectIcon($(this));
});
Added a function to perform the icon selection. Note: remove the return; statement and adjust the post so it will work for your application.
function selectIcon(e){
var selection = e.attr('class');
$('#selectedIcon')
.removeClass()
.addClass(selection)
.show();
$("#iconSelector").hide();
return;
$.ajax({
url: 'urltowebsite',
type: 'POST',
data: { selectedIcon: selection }
});
}
Added a UI element to show the user what icon they selected and modified the CSS slightly to accommodate the above changes.
You need a server-side script to save the data. If you do have a script for that, you can use it like that:
$("#iconSelector span").click(function () {
var xthis = $(this);
xthis.click(function(){
$("#iconSelector").hide();
$.post('/echo/html','icon='+$(xthis).attr('class'),function(){
$(".iconDisplay").html(xthis.get(0));
});
});
});
Full fiddle: http://jsfiddle.net/6NVpL/45/
PS. Change /echo/html to your save script name.
JS:
$(document).ready(function(){
$("#loader").load("external.html");
$("#buttonClickText").live('click', function() {
$("#buttonClickText").text("Text changed after button click.");
});
// MYSTERY FUNCTION
$("#pageLoadText").text("Text changed after external HTML was loaded.");
//
});
External HTML:
<div id="buttonClickText">
This text changes when clicked.
</div>
<div id="pageLoadText">
This text should have changed when external HTML was loaded, but didn't.
</div>
Main HTML (just showing the relevant tag):
<div id="loader"></div>
Also, I know .live() is deprecated for jQuery 1.7+, I'm guessing the solution will be similar using .on()
Thanks!
from http://api.jquery.com/load/:
This method is the simplest way to fetch data from the server. It is
roughly equivalent to $.get(url, data, success) except that it is a
method rather than global function and it has an implicit callback
function. When a successful response is detected (i.e. when textStatus
is "success" or "notmodified"), .load() sets the HTML contents of the
matched element to the returned data.
Just pass a function as the second argument.
$('#result').load('ajax/test.html', function() {
alert('Load was performed.');
});
below is the solution for the issue:
$(document).ready(function(){
$("#loader").load("external.html", function()
{
$("#pageLoadText").text("Text changed after external HTML was loaded.");
}
);
$("#buttonClickText").live('click', function() {
$("#buttonClickText").text("Text changed after button click.");
});
});
Several posts on stackoverflow say to use live to select dynamically generated html. However you need an event like click to use live. What do you do if you just want to use .val or .html and change the element instead of adding an event handler?
(Solution with live: Jquery not working with dynamically created table)
I am generating code through:
$.ajax({
url: "/JobFeed/JobFeed.php",
type: "POST",
dataType: "html",
success: function(html) {
$("#jobFeed").html(html);
}
});
The html once generated looks like this (with the form repeated several times):
<div id="jobFeed" class="contentContainer">
<div id="jobs">
<div class="job">
<form class="subscription">
</form>
</div>
</div>
</div>
I then add inputs with:
$("form .subscription").html("<input ... />");
You can use a plugin called "livequery" and do something like this:
$(selector).livequery(function(){
});
As you can see, there is no click or other event that you must use.
Plugin link: http://docs.jquery.com/Plugins/livequery
On github: https://github.com/brandonaaron/livequery
Hope this helps :-)
i have a form having two submit buttons.
one for creating a user and other for logging in an existing user.
how can i fire two different servlets from these two buttons keeping them in one single form??
like if create button is clicked then create.java is executed
and if login button is fired then login.java is executed
If you want to go with JavaScript, this is an example using jQuery ajax:
$(document).ready(function() {
$('#id_of_your_button').click(function() {
// do some stuff here, e.g.
var str = $("#your_html_form").serialize();
$.ajax({
type: "POST",
url: "url_to_your_file",
data: str,
success: function(msg) {
//...
}
});
}
// prevent the default action, e.g., following a link
return false;
});
});
EDIT:
if you want to do it without JavaScript, you can do it like <input type="button" value="register">
Otherwise, if you want the form submitted right away, you could only take two forms, or use JavaScript (also in other ways, as you could e.g. change the action-url with JavaScript depending on which button the user clicks, etc)...
... but there is only one "Submit" button allowed per HTML-form.
Check this tutorial about the onclick event for HTML buttons: Here
Example:
<script type="text/javascript">
function copyText()
{
document.getElementById("field2").value=document.getElementById("field1").value;
}
</script>
<button onclick="copyText()">Copy Text</button>
Source