i want only values inside my ISBN through my HTML Code
<div ISBN="9381015889">
Java Script Code
($("#ISBN").val());
i am not getting value of ISBN=9381015889
you can get this by using jquery data
HTML
<div data-ISBN="9381015889" id="ISBN">
Jquery
$("#ISBN").data('ISBN');
Put id attribute in the html div. ISBN cannot be attribute. So make id as attribute and name it as ISBN like below.
<div id="ISBN">9381015889</div>
Use this to access
$("#ISBN").html();
as .val() is used if you want to take from form fields like text field or drop down.
try this:
alert($(this).attr('ISBN'));
Try this:
HTML
<div ISBN="9381015889" id="ISBN">
Jquery
$("#ISBN").attr('ISBN');
You forgot to specify an ID to the div.:
<div id="ISBN" data-isbn="9381015889">
You can access the ISBN number via: $("#ISBN").data("isbn")
Reff: Jquery
Without modifying the html code, the css selector can be adapted: div[ISBN] instead of #ISBN.
Try this snippet:
document.write( $('div[ISBN]').attr('ISBN') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div ISBN="9381015889">
You can user data-ISBN instead of plain ISBN and include an id attribute for div tag
Then you can retrieve it using javacript like this
<div id='isbndata' data-ISBN="9381015889">
var selector= document.querySelector('#isbndata');
alert('Info is :' + selector.dataset.ISBN);
You can also use jquery's data attribute:
$('#isbndata').data('ISBN');
Links which can be helpful to u
http://api.jquery.com/data/
https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes
Hope this helps
Related
I am pulling my id's from a database with a foreach loop in a html file using
<div id="<?php print ($schedule['boxA']); ?>">
The question is how to use id's generated this way in a jQuery script?
<script>
jQuery('I NEED TO INPUT ID HERE').datetimepicker({
datepicker:false,
format:'H:i',
step: 15,
});
</script>
I would (and do) use a different pattern. You don't need to use the ID to reference an element with jQuery, so you can add a datetimepicker widget more generically by using the class selector.
HTML (inside foreach loop):
<div class="timepicker" id="<?php echo $schedule['boxA']; ?>">
JavaScript (only once):
<script>
jQuery('.timepicker').datetimepicker({
datepicker:false,
format:'H:i',
step: 15,
});
</script>
You would use the generated id's the same way that you always do in jQuery.
jQuery('#generated_id') // replace 'generated_id' with the id supplied by PHP
If you are wanting to echo the id's you could do something like this when the page is being rendered -
jQuery("#<?php echo $schedule['boxA']; ?>").datetimepicker({
Bad idea.
I would suggest something like:
<div id="boxA" data-id="<?=$schedule['boxA']?>">
Or similar. The ID should be constant and known - that's what makes it an IDentifier. data-id can then be used to hold the server's ID reference to the thing in question.
is it possible to somehow send $_POST[] data via a <a> tag? because it seems to automaticly execute the $_POST[] once the page loads rather than when I press the Anchor tag.
edit:
so what I'm trying to achieve is something like this:
I want an anchor tag which would normaly go to somepage.php?variable=something
but instead of using $_GET[] I want to send the variable via $_POST
Nothing in HTML will cause a link to trigger a POST request or encode data in the request body.
You can bind a JavaScript event handler to a link, cancel the default behaviour and then send a POST request by programmatically submitting a form or using XMLHttpRequest. This generally isn't a good idea and you should use a submit button instead (which you can style to look like a link if you really, really want to).
You can achieve this using jQuery and a HTML form
HTML:
<form method="post" name="redirect" class="redirect">
<input type="hidden" class="post" name="post" value="">
<input type="submit" style="display: none;">
</form>
Button: (html)
<a href='javascript:void(0)' class='button' var='DATAHERE'>sometexthere</a>
Javascript, or rather said jQuery:
$(".button").click(function() {
var link = $(this).attr('var');
$('.post').attr("value",link);
$('.redirect').submit();
});
this jQuery code listen's to any clicks on the items with the class button attached to them,
and reads out their "var" value, basicly you could use any kind of HTML element using this method as long as they have the button class attached to it.
Answer is no. What you can do is set the value of an input type when the <a> tag gets clicked. Using Javascript.
You could get hold of the query string from the href attribute of the anchor tag (you would need to parse the href and get hold of the string on the right hand side of the & symbol) and then post it using javascript or jquery (easier to use jquery).
http://api.jquery.com/jquery.post/
Would have to ask why you would want/need to do this?
This question is related to another one I asked yesterday and its link is:
Parse HTML in jquery through ajax into elements and replace each corresponding on page
Basically i want to call a php page with ajax and replace the elements in the response with the corresponding ones on the page. I got the following function in an answer:
$(data ).filter('.maindiv').each(function (index)
/* "this" is the current div in response*/
$('#'+this.id).replaceWith(this);
});
The above function woeks well when the div I want to replace has a regular id= but if use a custom attribute like gid= for example it won't work. How can i fix this??
Thanks
Use attr for custom attribute instead of using this.id you can use $(this).attr("YourAttr");
$(data ).filter('.maindiv').each(function (index)
/* "this" is the current div in response*/
$('#'+$(this).attr('gid')).replaceWith(this);
});
You can select node with a gid attribute with:
$('[gid]').replaceWith(this);
you can even be more precise by selecting only the node which has the gid value you want
$('[gid="hello"]').replaceWith(this);
Hope it helps
For data you can use a custom attribute. HTML5 specificies the use of a data- attribute. The cool thing is that this works in HTML4 too! jQuery can read it by using the data method.
I'll recommend:
<div class="maindiv" data-grid="myGrid">...</div>
$(data).filter('.maindiv').each(function (index)
$('#'+$(this).data('gid')).replaceWith(this);
});
I have a button (styled as button with CSS). Next to it is an input field. I would like the button value to change dynamically as the user types a value in the textfield. What is the simplest way to do this?
My form:
<form>
<div class="span-3" id="bid_btns">
<div id="bid_button">
<?php echo $somethingwhichshouldchange; ?>
</div>
</div>
<div class="span-3 last" id="bid_btns">
<div id="bid_field">
<input type="text" class="title" name="bid_field" id="bid_field" maxlength="5"/>
</div>
</div>
</form>
PHP is a server side language, so you'll never be able to change a variable locally without re-serving the page. You need to investigate javascript to do what you're doing.
If you want to make the button label/value change as you type, PHP is not going to help. You need a script running in the browser - JavaScript. If you use jQuery (you did ask for the simplest way, and jQuery makes this much simpler), you need some code to change the button as the input value changes.
$(document).ready(function() {
$("#bid_field").keyup(function(){
$("#bid_btns").text($(this).val());
});
});
This bit of code will change the html inside the #bid_btns button to whatever is typed into the #bid_field input. If you're not familiar with jQuery, you can learn more by reading this article: http://docs.jquery.com/How_jQuery_Works
Note - edited per BenM's siggestion.
You can use jQuery to achieve this. You just need to bind a function to the onkeyup event:
http://jsfiddle.net/Vt6N9/
if i have a text field like this:
<input type="text" name="quantity" id="txtquantity">
what i am trying to figure out is how can i grab the value of that text field and append it to a link like this:
index.php?quantity=(the value)
if this is done with jquery how would i do it?
Im not sure if you are trying to do this on the page the input gets posted to, or the current page. If you are trying to do it on the page the form gets posted to you would need to use PHP like the following.
<?php
echo '<a href="'.$_GET['txtquantity'].'" />';
?>
However, if you are trying to do it before the form is submitted then using jQuery you would do it like this,
$("a").attr("href", $(this).attr("href") + $("#txtquantity").val());
You can do like:
<a id="your_link_id" href="whatever">A Link</a>
jQuery:
$('#your_link_id').attr('href', $(this).attr('href') + $('#txtquantity').val(););