Add Heading Before dynamically created form field Jquery .insertBefore - php

I have a form that is dynamically created, so I can't edit the layout of the form itself. However, I want to add a title above one of the text input fields in the form. I have given that field a unique css ID and am using the following script to try and add my heading before it:
<script type="text/javascript">
$('<h5 class="form_title">Calculate Return</h5>').insertBefore('#price');
</script>
For some reason this won't work though, can anyone think why that might be. Here is the page in question
http://www.theres-a-thought.com/client-yourdream/?page=property_submit

Add document ready to your code -
<script type="text/javascript">
$(document).ready(function() {
$('<h5 class="form_title">Calculate Return</h5>').insertBefore('#price');
});
</script>

Looking at your page, I don't see where #price is defined. Maybe it is added later via JS and i missed it..
You are using jquery in no-conflict mode, and you need to wrap your code in document ready..
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('<h5 class="form_title">Calculate Return</h5>').insertBefore('#price');
});
</script>
Running this; however, has no affect on the page(from what i can see).
The code below works because the ID exists. It isn't what you are trying to accomplish, but it should get you started.
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('<h5 class="form_title">Calculate Return</h5>').insertBefore('#property_name');
});
</script>

Related

How to place an event for multiple items?

I have a doubt. All right. I have a post in which users post comments and next to each post have a button belonging to each of them. Well, as I get when I click on each of these buttons me out a popup.
With that event ?. An event for several elements. But that is the same for all those buttons.
ahhh. These buttons are generated or printed with php, when the user posts something, that something has a button. Help!!!
HTML
<a id="popup" class="globes_post_giving">♠</a>
JS
<script type="text/javascript">
$(document).ready(function(){
$("#popup").on({ click:function(e){
alert('hola');
} });
});
</script>
The jQuery code seems not good.
There are syntaxes errors.
Here is my two cents :
<script type="text/javascript">
$(document).ready(function(){
$("#popup").click(function(e){
alert('hola');
});
});
</script>
In order to be used with multiple buttons you'd better use a class instead of an id and set the click event on it.
HTML
First link
Second link
Third link
JS
$(document).ready(function(){
$(".popup").click(function(e){
alert('hola');
});
});
That's all I can do without more code.
Hope it helps.

jQuery's GET is acting up for me. Using CodeIgniter. It returns source code for a view instead of one value

I am trying to test the jQuery GET method, but what I am getting as result is completely insane. All I am trying to do is call a function from my controller with jQuery and then display the echoed value in a div within my view. Below is my code and finally a breakdown of the problem I am encountering.
Controller
public function generate_suggestions2() {
echo "James";
}
View views\suggestions_v.php
<body>
//This DIV is used a button to call the jQuery function
<div id="next_btn">
<p>Click here to display a name</p>
</div>
//In this div the value retrieved by the jQuery should be displayed
<div id="listB"></div>
//This is the function that calls the function within my controller
<script type="text/javascript">
$( "#next_btn" ).click(function() {
$.get("core/generate_suggestions2/",
function(data) {
$( "#listB" ).html(data);
});
});
</script> //For some reason I need to put the script at the end of the body. When it's in the head nothing happens when I click the button. Also something I do not understand.
</body>
Now the problem is that when I click the DIV next_btn it does NOT display the James in the DIV listB.
Instead it populates the DIV listB with my source code from my main view views\core_v.php
I have no idea how this is even remotely possible, so please if you have a clue or even better you know what I am doing wrong please tell me. I am trying to get this to work for the past three days without any success or progress :(
Try using this code in your view
<script type="text/javascript">
"$( "#next_btn" ).click(function() {
$.get("<?php echo site_url().'/core/generate_suggestions2';?>",
function(data) {
$( "#listB" ).html(data);
});
});"
</script>
Also delete the </script> tag just right before this block comment.
Regarding this comment you have:
//For some reason I need to put the script at the end of the body. When it's in the head nothing happens when I click the button. Also something I do not understand.
It is because the DOM is not yet fully loaded and Javascript executes its code when the browser has finished loading the DOM you are referring to. So you either have to put it after the DOM element you want to edit, like you do now or you could use $(document).ready(function(){
}
); .
Read more about it here
This line is pretty strange:
</script><script type="text/javascript">
Try to delete first closing tag 'script' on it.

load div content from external file

I've tried several options to try and load the content from a div on one page with id="container" into a div on a different html page id="landing".
I used the answer here and also lots of variations of it
Load content of a div on another page
I've run a test and Jquery is being loaded.
The test files are here http://www.salcombeyurts.co.uk/test/landing.html
My code looks like this:
<script type="text/javascript">
$('#landing').load('source.html #container');
</script>
This will eventually end up on a PHP page. Part of a Joomla site.
You run the script before the #landing div is defined.
Run your code after the DOM ready event
$(function(){
$('#landing').load('source.html #container');
});
It seems like the suggestion you got was to do an AJAX request and load the entire page into the #container div on the current page, which is not a bad idea. What you seem to be trying to do, on the other hand, is load the page and then get the content of a div inside that page and put it in a container div on the current page, which is overly complicated and a bad solution to what ever the problem is.
Here is my solution none the less
$(function() {
$.get('page with the div you want', function(data) {
var content = $(data); //turn the page into a jquery object
var div = content.find('#div'); // this is the div you want the content from
$('#container').html(div.html()); // this will set the content of the current div
});
});
I would move your script to the bottom of the page and replace it like so.
Original:
<script type="text/javascript">
$('#landing').load('source.html #container');
</script>
New:
<script type="text/javascript">
$(document).ready(function() {
$('#landing').load('source.html #container');
});
</script>
Note the space removal between source.html and #container.

jQuery toggle() doesn't work inside included (using PHP) code.

I have the following code defined in order to hide certain elements of a list:
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".done").toggle();
});
});
</script>
Basically, any < button > element being clicked will execute the toggle() function on any element with the "done" class. I know this works, because it works on some of my buttons. I have a page made up of several included files (using PHP include()). Usually, the javascript works in and out of these included files, but for some reason if I put a button inside one of them, it doesn't work - the function only works for buttons placed on the document where the script is defined. Is this always the case, or am I doing something wrong?
try using jQuery live function:
$("button").live('click', function(){
$(".done").toggle();
});
Try changing:
$("button").click(function(){
to:
$("button").live('click', function(){
This will make the event bind to any button, no matter when they are added. If you are using .live, then you don't need it inside a $(document).ready( block, as .live will add the event when the element is added.

image drag and display

How image drag and display corresponding row and how take that image's name when sumit form we have four images on topof the page.each images have each value.then below that display some words row by row.whenever we drga tha image from the top to the corresponding words,then we can identify that image name,corresponding words when submitting the form.
my jquery code is here
<SCRIPT>
$(document).ready(function(){
$("#move").draggable();
});
</SCRIPT>
<SCRIPT>
$(document).ready(function(){
$("#move1").draggable();
});
</SCRIPT>
<SCRIPT>
$(document).ready(function(){
$("#move2").draggable();
});
</SCRIPT>
<SCRIPT>
$(document).ready(function(){
$("#move3").draggable();
});
</SCRIPT>
"image drag and display" indicates user interaction. This can't be done in PHP. Interaction like this needs to be done client-side which means javascript.
PHP is completely server-side.
EDIT: When I wrote the answer there was no mention of javascript or jQuery so I was pointing out that php couldn't do what he was asking since it's only tagged php (I'm about to edit that).
A better answer would be that the image would need to be a part of the form, otherwise it won't be submitted to php.

Categories