How to Embedded JQuery code in a php variable? [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to embedded the below jquery code in the following php variable $var
JQUERY Code:
<img src="images/image.png" id="Image1" alt="" style="width:30px;height:30px;">
PHP Code:
<?php $var = 'my_jquery_code'; return $var; ?>

You just need to escape the single quotes:
<?php
$var ='<img src="images/image.png" id="Image1" alt="" style="width:30px;height:30px;">';
return $var;

The only reasonable scenario I can think of where you would want to use onclick would be if you're dynamically loading the anchor element.
If so, this is probably better.
[script]
$(document).on('click', '.my_class_here', function() {
$('jQueryDialog1').dialog('open');
});
[/script]
[link_template]
...
[/link_template]
Then you would use AJAX or whatever voodoo you like to retrieve the link template (maybe while passing it some variables?) and add it to the page.
Alternatively, you can use $(document).find('.my_class_here').on('click', function... , but this is bad, performance wise.

Related

how to render array of only urls in react js? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am making a web app which is getting data in the form of http url which are of images eg:
let array = ["https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m000.jpgtoken=e7bf6b3b8fa25c218502b913a9c722ec374229d6&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m001.jpg?token=3cd1bb77873151a1311e0f87d3cdd68100045326&ttl=1633968000", "https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m002.jpg?token=74b31d27c19d32b1abb3db47eeb97a5cb4f381c7&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m003.jpg?token=ff06277b89a5764deccae021411342de9947f130&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m004.jpg?token=a5641dc8ed4d61de17793f94a45e69ab38f5fce3&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m005.jpg?token=659f9f64adad8356006bc939365b492004bedc12&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m006.jpg?token=46641b886d2eef796860c5d29e2959e4cb621945&ttl=1633968000","https://zjcdn.mangafox.me/store/manga/31721/142.0/compressed/m007.jpg?token=6c8ff51fd06f5a0171bde3e3bee2c68606985918&ttl=1633968000"]
and I want to render them on page I have used following code:
`{
arr.map(e=>{
return <img src={`${e}`} alt="" />
})
} `
and this:
arr.map(e=> (<img src={e} alt="" />))
but the problem is it is only rendering one image instead of rendering all images.
I think you have got some issues with your array as it would appear is has some format problems. Here is the fix
{
array.map((el,i )=> <img key={i} src={el} /> )
}

Passing variable from Javascript to href in Codeigniter modal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I pass variable value from Javascript and use it as a variable in <a href> bootstrap modal ? for access controller and process with the spesific / targeted ID
Answer (Trick) support by #MikO
This is <a>tag in bootstrap modal looks like :
<a class="btn btn-primary" name="gotopick" id="gotopick" href="<?php echo site_url('onpartners/picking'). '/{passId}'; ?>">Pick</a>
Then, in your JS, you can replace that {passID} with the actual value of Variable with something like this :
$("#gotopick").attr("href", $("#gotopick").attr("href").replace('{passId}',yourvarvalue));
Thanks for sharing #MikO, hope your answer can help another people with same problem too here
Sharing is caring :)
You can add more attribute in your tag <a> like
<a data-id="{{ $pass_id }}" class="btn btn-primary" name="gotopick" id="gotopick" href="<?php echo site_url('onpartners/picking'). '/' . $pass_id; ?>">Pick</a>
So, you can get your data in your jquery like
var id = $('a#gotopick').data('id');

Call jquery tag in javascript function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
<script>
function addMoreAns() {
$(".add").append("<h1>Hi</h1>")
}
</script>
It is my jquery tag in javascript function.
jquery tag don't work under javascript function
You must have three things for your code to work:
You must make sure to have the JQuery library referenced prior to your use of any JQuery code.
In the HTML, you must have an element that is allowed to contain content with an add class.
You must invoke your function somehow. Just writing a function isn't the same thing as running it. You have to decide when the function should run and set up your code to invoke it at that point.
In the example below, the function is invoked every time the button is clicked.
document.querySelector("input[type=button]").addEventListener("click", addMoreAns);
function addMoreAns() {
$(".add").append("<h1>Hi</h1>")
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Click to Add">
<div class="add"></div>
You need to load the jQuery library in the head of your page
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

Hide div if database contains value - Angular [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to check if a MySQL database contains a specific value. If it is true I want to hide a HTML <div>. Is this possible?
Can I hide elements with CSS & JS, or what should I use to hide the div?
Also, How would we add it in the Div like a NgIF statement
Thanks!
If you are seriously thinking about hiding the element with JavaScript (due to some kind of restriction or requirement). You can use following method.
CSS + Javascript + PHP way:
If you are thinking more like the Run code snippet section in Stack Overflow. Show expanded ONLY if it is set in database.
Then in that case you can implement something like this
$(function() {
$("#btnToggle").on("click", function() {
$("#comments").toggleClass("hide");
});
});
.hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Hide using CSS way. Hide class can be echoed from your PHP based on the database value -->
<button type="button" id="btnToggle">Toggle hide/show</button>
<div id="comments" class="hide">Comment section to hide</div>
Try something like this in your PHP file. I have included an example of an item that is always rendered, and something that is conditionally rendered.
<div id="userinfo">
<?php // This is always rendered by the server ?>
Username: <?php htmlentities($row['username']) ?>
<?php // This is rendered by the server if the column is true ?>
<?php if ($row['admin']): ?>
<div class="admin">(admin)</div>
<?php endif ?>
</div>
Something along the line of
if (your statement condition) {
echo '<script type="text/javascript">document.getElementById(DIV_ID").style.display= "none";</script>';
exit;
}

How on one click another page div value change using JavaScript? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two page page1.php, page2.php and one JS file Control.js.
In page1.php
HTML :
<img src="images/b.jpg" width="31" height="26" id="imgClick1" onClick="return changeImage1(this)" >
In Control.js
function changeImage1() {
document.getElementById("imgClick1").src = "images/b.jpg";
document.getElementById('num1').style.color = "#fff";
document.getElementById("text1").innerHTML = "Hello friend";
}
In page2.php
<div id="text1">
Hello world
</div>
So using JavaScript I am trying to write "hello friend" in the div "text1" of page page2.php when I click the image or the div of page1.php.
Is there any possible way to use JS to solve this problem?
Can we use any how document.getElementById("text1").innerHTML or something like that in the program?
I suggest you make use of the include function of php (provided you absolutely want to keep your text1 div on a separate page), like this on your page1.php file:
include('page2.php');
Make sure that when you do that, page1.php and page2.php are in the same folder, otherwise you may define a path to it like this:
include('/path/to/page2.php');

Categories