This looks very simple but I have little experience with jQuery and I can't wrap my head around it.
Let's say I have a dynamically generated HTML table, and in each row of this table is a link:
<a id='edit'>Edit User</a>
Now, the link should call a function with each user's ID as a parameter. I could do that inline liek this:
<a id='edit' onClick='editUser(<?php echo $row['id']; ?>)'>Edit User</a>
But how do I do this in jQuery?
I can call the function like this:
$('a#edit').click(function () {
editUser()
return false;
});
But how do I pass the ID to this function? I know I could first stick it into a hidden field and then get it from there by element id, but surely there's a better way?
I realize all the links would have the same id this way, so should I dynamically create the link ids by appending the user id? But then how do I call the jQuery?
ids must be unique throughout the entire HTML. So you could use a class selector and HTML5 data-* attribute:
<a class="edit" data-id="<?php echo $row['id']; ?>">Edit User</a>
and then:
$('a.edit').click(function () {
var id = $(this).data('id');
// do something with the id
return false;
});
Use data-* attributes to pass parameters.
<a class='edit' data-id='<?php echo $row['id']; ?>'>Edit User</a>
$('a.edit').click(function () {
editUser($(this).data("id"));
return false;
});
As Curt mentionned, the data-id is the way to go, if you're using HTML5. If you're using HTML4, I would pass this in the ID of the link :
<a id='edit-321' class='edit'>Edit User</a>
Then you can do this (and use event.preventDefault() rather than return false !) :
$('a.edit').click(function (evt) {
editUser($(this).attr("id").substring(5));
evt.preventDefault();
});
Related
I am using a number of HTML5 audio players on one page and I need a means to control them individually. I have so far been using:
<?php $unique = uniqid(); ?>
to generate a unique number. I have them been able to successfully apply this to my player:
<button class="bb-play" onClick="play-<?php echo $unique; ?>()">Play</button>
I am able to apply this within some jQuery, within the function as follows:
function play() { document.getElementById('player-<?php echo $unique; ?>').play(); }
My question is, where my unique number is say 56f295fbe6be3 how can get I get the function play() to appear instead as play-56f295fbe6be3() ?
I appreciate any help you can provide.
Trying to write a function with a unique number in its name is a bad approach. You should rather write a unique function that is able to handle them all.
<script>
function play(that) {
var playerId = that.id;
document.getElementById('player-' + playerId).play();
}
</script>
<button class="bb-play" id="<?php echo $unique; ?>" onClick="play(this)">Play</button>
From a semantic perspective, we're assigning to the <button> element an attribute that says what's the player id and it would make more sense to put it into a data attribute. It comes easier to use jQuery then:
<button class="bb-play" data-player-id="player-<?php echo $unique; ?>">Play</button>
<script>
$("button.bb-play").click(function(){
var playerId = $(this).data('playerId');
$("#" + playerId).play();
});
</script>
HTML:
<button class="bb-play" onClick="play(<?php echo $unique; ?>);">Play</button>
Javascript:
function play(id) { document.getElementById('player-'+id).play(); }
If the buttons and the player are places inside the same parent element, you could work without id's.
<div class="player">
<media .../>
<button class="bb-play">Play</button>
</div>
and the js code
$('.bb-play').on('click', function() {
$('media', $(this).closest('.player')).get(0).play();
});
Why not pass the id number as a variable to the function?
function play_video(id) {
document.getElementById('video-' + id).play();
}
And in the HTML use:
onClick="play_video(<?php echo $unique; ?>)" id="video-<?php echo $unique; ?>"
// onClick="play_video(56f295fbe6be3)" id="56f295fbe6be3"
Then you can just use play_video() for all videos.
I have this jquery
$(".waitingTime .button").click(function () {
alert("Ema");
});
I have a a tag like this:
Can I do the same href action in the jquery function?
Many Thanks
yes this is possible and has nothing to do with Laravel.
There are different possibilities. If your query is embedded within the same laravel view, you put the URL directly in your jQuery code, for example like this:
$(".waitingTime .button").click(function () {
window.location.href = "{{URL::to('restaurants/20')}}"
});
But I think the best option is to add the URL on your button tag as a data attribute and then let jquery go to that URL. That way you can make your buttons more dynamic and have more capsulated code.
One example might be:
<div class="waitingTime">
<button class="button link-button" data-href="{{URL::to('restaurants/20')}}">
Click me
</button>
</div>
$(".link-button").click(function () {
window.location.href = $(this).data('href');
});
That way you can always give a button with the class link-button a data-href attribute with the URL you want to open when the button is clicked and don't have to add additional jquery.
Just output php in your javascript
$(".waitingTime .button").click(function () {
window.location.href = "<?php echo URL::to('restaurants/20'); ?>";
});
if need variable from js can use this
if(data.cookies == 0){
location.replace("{{ route('delivery') }}?id="+id);
}
This might be a popular question but I couldn't find the answer I'm looking for.
Anyway What I want to do is pass an option value in the parent window to a popup window so I can display some thing in the popup according to the selected option.here is my code of the parent window.
<select name="itemSelect" id="itemSelect">
<option>chair</option>
<option>table</option>
</select>
<a class="button" href="#" onclick= "MyWindow=window.open('form3.php','MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,width=500,height=650'); return false;">enter data</a>
Using jQuery, you could do something this.
value = $('#itemSelect option:selected').val();
window.open('form3.php?value='+value+', MyWindow
You could then look for $_GET['value'] on the PHP page and process it!
<a class="button" href="#" onclick= "MyWindow=window.open('form3.php?itemSelect='+document.getElementById('itemSelect').value,'MyWindow','toolbar=no,location=no,directories=no,status=no, menubar=no,scrollbars=no,resizable=no,width=500,height=650'); return false;">enter data</a>
Then in form3.php
echo $itemSelect = $_GET['itemSelect'];
You can do this via special onclick handler by adding your value via hash or url param:
<a class="button" href="#" onclick= "openNewWindow()">enter data</a>
JS:
function openNewWindow() {
var your_select = document.getElementById("itemSelect");
var value = your_select.options[your_select.selectedIndex].value;
MyWindow=window.open('form3.php#' + value,
'MyWindow',
'toolbar=no,location=no,...,width=500,height=650');
// ... or
// MyWindow = window.open('form3.php?select_value=' + value, ...
return false;
}
You want to pass the selected option to a pop up window for better interaction?
It would be easier to use alert() in js; using onclick handler you can call a function in js which then displays the pop up by using alert(document.getElementById().value=variable)
In a PHP page i have several rows of one table like this
echo '<tr><td>Click</td></tr>';
The $id is dynamically generated from a database
So I want to define the function in jQuery but to pass the parameter to the jQuery function.
For each button I click there will be another parameter passed
Why not use the ID as an identifier for the link like this:
Click me
In jQuery you can bind to the onclick event like this:
// Execute on load
$(document).ready(function(){
// Bind to click
$('a.myjquerylink').click(function(){
// Get the id
var id = $(this).attr('id');
// Do something with the id.
doSomething(id);
});
});
What exactly do you want to do ?
Here's a sample function (it's not using jQuery!) to alert the user that the linked has been pressed and to stop propagating the event, so that it doesn't jump to another page on click
<script type="text/javascript">
function myFunction( param ) {
alert('The button with the id ' + param + ' has been pressed!');
return false;
}
</script>
Well in a dirty way you can assign your id's in rel tag like this:
echo '<tr><td>Click</td></tr>';
than you can search for mybutton class in jquery an add events to it:
$("a.mylink").click(function(){
alert($(this).attr('rel'));
});
So in this case $(this).attr('rel') should be your ID.
As other poster started saying, bind a function to an event. Say you assign a css class to your a tags to make it easier:
echo '<tr><td><a class="specialLinks" href="#" onclick="myFunction('.$id.')">Click</a></td></tr>';
Then you would bind to your class like this:
$('.specialLink').bind('click', function() {
this.preventDefault();
alert($(this.attr("id"));
});
you need to modify your html a bit:
echo '<tr><td><a class="someclass" href="#" id='".$id.'">Click</a></td></tr>';
then you can call it by it's class in JQuery and do what you want:
"$(this)" will be a reference to the clicked item.
$(".someclass").live('click',function(e){ e.preventDefault(); alert($(this).text())});
I have a page where ID is generated dynamically and be fetch from database and I put the result inside <a> tag :
<?php while($row = mysql_fetch_array($result))
{ ?>
ID Number 1<br />
ID Number 2
<?php } ?>
and when user click the link, the javascript myfunc() function will be trigger.
function myFunc(){
$("#div").load("get_id.php?","id="+"SHOW THE $row['id'] HERE"); }
But I don't know how to retrieve href value and put it inside the load() method. Can someone show me the correct way?
Thank you
-mike
Make sure that you generate a complete href attribute:
ID Number 1
and then attach a click handler unobtrusively (don't mix markup and javascript):
$(function() {
$('a').click(function() {
$('#div').load(this.href);
return false;
});
});