Welcome to WebmasterWorld Guest from 54.226.133.245

Forum Moderators: open

Message Too Old, No Replies

Jquery Form plugin : dynamic target/multiple forms

     
11:41 pm on Sep 29, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


Hello, I am using the form plugin from [malsup.com...]

I am trying to set the target div to be updated dynamically. At the moment this is what the code looks like but instead I want to pas a different value for target through a hidden form field, so the #target2 can be dynamically changed to #target20, #target40 etc. Thanks.


<script type="text/javascript">
$(document).ready(function() {

$('.htmlForm').ajaxForm({
target: '#target2',

success: function() {
$('#target2').fadeIn('slow');
}
});
});
</script>


Thank you.
12:26 am on Sept 30, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10



$(document).ready(function() {
var target = $('#target').val();

$('.htmlForm').ajaxForm({
target: target,

success: function() {
$(target).fadeIn('slow');
}
});
});

Then in your html, just give your hidden input the id specified in the code above:

<input id="target" value="target2">
12:37 am on Sept 30, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


Thank you Fotiman, I updated the code to


<script type="text/javascript">
$(document).ready(function() {
var target = $('#target').val();

$('.htmlForm').ajaxForm({
target: target,

success: function() {
$(target).fadeIn('slow');
}
});
});
</script>


<form class="htmlForm" action="ajax-data.php" method="post">
<input type="hidden" id="target" value="target2">
Message: <input type="text" name="message" value="Hello HTML" />
<input type="submit" value="Echo as HTML" />
</form>


<div id="target2">div id=target2</div>



But when the form is submitted the div with target2 id does not update. What did I do wrong? thanks.
9:31 pm on Oct 1, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


Could anyone take a look into this please?
1:53 pm on Oct 2, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10



But when the form is submitted the div with target2 id does not update.

Do you see the target div fade in when you submit the form? Because that's all it looks like you're attempting to do.
9:51 pm on Oct 2, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


Thanks, not trying to just to fade out/in a div. I am trying to load the html ajax-data.php echos. With the code above nothing happens at all when the forms gets submitted.
9:57 pm on Oct 2, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


This code will do submit the form and refresh the div with the html from ajax-data.php into div with id=target. however I want to load the data into a div id that is indicated into thte form's hidden field (target) which in the code bellow is set to z22.


<script type="text/javascript">
$(document).ready(function() {

$('.htmlForm').ajaxForm({
target: target,
delegation: true,

success: function() {
$(target).fadeIn('slow');
}

});
});
</script>


</head><body>

<form class="htmlForm" action="ajax-data.php" method="post">
<input type="hidden" name="target" value="z22" />
Message: <input type="text" name="message" value="Hello HTML" />
<input type="submit" value="Echo as HTML" />
</form>

<div id="target">div id=target</div>

<div id="z22">div id=z22</div>
5:12 pm on Oct 3, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4965
votes: 10


The problem was that the value of your target element did not start with '#', so the code just needs a slight modification to make sure it's a valid id selector. Here's a working example:


$(document).ready(function() {
var target = $('#target').val();
// Make sure target is a valid id selector (starts with #)
target = (target.indexOf('#') === 0? target : '#' + target);

$('.htmlForm').ajaxForm({
target: target,

success: function() {
$(target).fadeIn('slow');
}
});
});
5:27 pm on Oct 3, 2013 (gmt 0)

Full Member

10+ Year Member

joined:Mar 23, 2001
posts:244
votes: 1


Perfect! works great, many thanks Fotiman. I appreciate your help.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members