I try to implement the following code:
html
<span id = "myset">school</span>
Javascript
var myset =$("#myset").val();
var ID=$("#data li:last").attr("id");
$.get('page.php', 'id='+ID+'&set='+myset, function(newdata){
$('#data').append(newdata);
});
In the page.php file, it doesn't receive the value of the $_GET['set'], but if I change
var myset =$("#myset").val();
to:
var myset ='school';
everything works fine in this case, what am I doing wrong, anyone could help me, thanks.
I also tried
var myset =document.getElementById("myset").value;
but it is still not working;
Span tags don't have a value, they have .innerText or .innerHTML or, with jQuery, .html() or .text()
"school" isn't the value of your span. It is the data of a text node inside your span. You can access it like this:
var myset = $("#myset").text();
Use var myset =$("#myset").text(); instead.
Related
I would like to loop through a page of and store every with the data attribute of "agencyname". Although, my JQuery code doesnt allow me to do so. Can someone take a look and let me know what I am doing wrong, thanks.
The HTML
First
The Jquery:
var myVals = [];
$('a').data('agencyname').map(function(){
myVals.push($(this).attr('value'));
});
Try this using attribute selectors.
var myVals = $('a[data-agencyname]').map(function(){
return $(this).data('agencyname');
}).get();
DEMO
I have a HTML string that I'm passing through a function and I want to be able to perform Jquery methods on that variable from inside the function - such as .attr('href') or .text(). I'm sure there is a simple solution for this and something more elegant then temporarily appending the DOM.
HTML
<div class="here"></div>
Javascript
link = 'Google';
// This works
$('.here').html(link);
works = $('.here').text();
console.log(works);
// This doesn't
not = link.text();
console.log(not);
http://jsfiddle.net/dfgYK/
You need to create a jQuery object from link in order to use jQuery methods on it. Try:
not = $(link).text();
DEMO: http://jsfiddle.net/dfgYK/1/
Depending on what you're doing with link, it might be beneficial to do this earlier in your code so that you can just use something like:
var $link = $(link);
console.log(link.text());
You can make a jQuery object that is not part of the DOM by passing a string in:
link = $('Google');
Then, jQuery methods will work on it:
var text = link.text();
Create the link with jQuery instead:
var link = $('<a />', {
href: "http://www.google.com",
text: "Google"
});
Then you can access it's properties with link.text() like you wanted.
I am dumping some CSS into a div and I am looking to format it so it is more legible. Basically what I want to do is insert a break tag after every semicolon. I have searched around for a while but can't seem to find something that quite fits what I am trying to do.
I have something like this...
HTML
<div class='test'>
color:red;background-color:black;
</div>
jQuery
var test = $('.test').text();
var result = test.match(/;/g);
alert(result);
And I have tried..
var test = $('.test').text();
var result = test.match(/;/g);
result.each(function(){
$('<br/>').insertAfter(';');
});
alert(result);
Also I have started a fiddle here.. Which basically just returns the matched character...
http://jsfiddle.net/krishollenbeck/zW3mj/9/
That is the only part I have been able to get to work so far.
I feel like I am sort of heading down the right path with this but I know it isn't right because it errors out. I am thinking there is a way to insert a break tag after each matched element, but I am not really sure how to get there. Any help is much appreciated. Thanks...
try it like this
var test = $('.test').text();
var result = test.replace(/\;/g,';<br/>');
$('.test').html(result);
http://jsfiddle.net/Sg5BB/
You can use a normal javascript .replace() method this way:
$(document).ready(function(){
$(".test").html($(".test").html().replace(/;/g, ";<br />"));
});
Fiddle: http://jsfiddle.net/SPBTp/4/
Use This CODE
var test = $('.test').text();
var result = test.split(';').join(';<br />')
http://jsfiddle.net/FmBpF/
You can't use jQuery selectors on text, it only works on elements.
Get the text, just replace each ; with ;<br/>, and put it back.
$('.test').html($('.test').text().replace(/;/g, ';<br/>'));
Try something like this :
var test = $('.test').text();
var result = test.replace(/;/g,";");
$('.test').html(result);
That should work if you stick it into your jfiddle.
The following snippet doesn't work.
var empty = $();
var divs = $("div");
empty.add(divs);
There is a div element in the HTML and it is added correctly to divs. But the divs collection is not added to the empty jquery object.
Any ideas what`s wrong with that?
.add won't change the original object. Try:
empty = empty.add(divs);
You can do
var empty = $.extend($(), $('div'));
Per Jquery doc,
The following will not save the added elements, because the .add() method creates a new set and leaves the original set in pdiv unchanged:
var pdiv = $("p");
pdiv.add("div"); // WRONG, pdiv will not change
I have a function
function toggleSelectCancels(e) {
var checkBox = e.target;
var cancelThis = checkBox.checked;
var tableRow = checkBox.parentNode.parentNode;
}
how can I get a jQuery object that contains tableRow
Normally I would go $("#" + tableRow.id), the problem here is the id for tableRow is something like this "x:1280880471.17:adr:2:key:[95]:tag:". It is autogenerated by an infragistics control. jQuery doesn't seem to getElementById when the id is like this. the standard dom document.getElementById("x:1280880471.17:adr:2:key:[95]:tag:") does however return the correct row element.
Anyways, is there a way to get a jQuery object from a dom element?
Thanks,
~ck in San Diego
Absolutely,
$(tableRow)
http://docs.jquery.com/Core/jQuery#elements
jQuery can take the DOM elements, try with:
$(tableRow)
or
$(checkBox.parentNode.parentNode)
You should be able to pass the element straight in, like this:
$(tableRow)...
I have tested this by creating a reference to a div, then passing it straight into jQuery and it creates the jQuery object for you.
You can call the jQuery function on DOM elements: $(tableRow)
You can also use the closest method of jQuery in this case:
var tableRowJquery = $(checkBox).closest('tr');
If you want to keep using your ID, kgiannakakis (below), provided an excellent link on how to escape characters with special meaning in a jQuery selector.
See this for how you should escape the id.
try:
var r = $(document.getElementById("XXXX----ID Of Your Row----XXXX"));
now, if document.getElementById doesn't return undefined you can use r as any regular jquery object.